cmilfo has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to make SOAP clean memory? I have a SOAP server (implemented with SOAP::Transport::HTTP, which uses SOAP::Lite, HTTP::Daemon, etc.) that returns the contents of many files.

One of the methods I've written returns a reference to an array so that it is encoded as an array when being returned (the client hitting my server is Java based). The array of file contents takes about 8 mB in memory. I notice, however, that 16 mB (over the ~6 mB that is consumed before the array is loaded) is being used by the server. My assumption is that the 8 mB extra is from the serialization process and that it copies the array in memory.

That is a side point, though. The memory is not freed after SOAP returns the data to the client. The 16 mB is held in memory for quite a long time. I called the server several times asking for different files until finally it returned the memory. That prompted me to try calling it N times to free the memory, it's not based on the number of calls. It does appear to be based on time or time between calls. It seems to top out at 25 mB. Then it doesn't use any beyond that.

I've read quite a bit of documentation and not found anything explaining how the memory works. Does anyone here know how? And does anyone know how to control when the memory is freed (I would like to free it as soon after the return as possible)?

Please let me know if you would like to see the code for the server.

Thank you!
Casey

Replies are listed 'Best First'.
Re: Does SOAP clean memory?
by djantzen (Priest) on Feb 08, 2002 at 20:49 UTC

    To clarify: you're not actually passing a reference over the wire, rather you're copying the entire contents of the array over to the client side. And this data must be serialized before it can be transferred, hence the extra 8mb on the server. Right?

    What it may be doing is caching that serialized data in case another client asks for it. This would explain why it is time interval-dependant. Is it possible to obtain a reference to the serialized copy? If so then you could manually undef() it with a call from the client side after it has been received.

    On another point, are you sure it's best to transfer the entire array? 8mb is quite a lot to pass by value unless you absolutely have to have it. What about wrapping the array in a class instance and returning a reference that the client could query for more specific entries? This will naturally involve greater frequency of network traffic, but depending on your needs it could be more efficient.

Re: Does SOAP clean memory?
by chromatic (Archbishop) on Feb 08, 2002 at 21:28 UTC
    Perl very rarely returns memory to the operating system. I'd say never, but have heard reports that the Linux malloc, in some cases, will do this.

    Unfortunately, the answer is that while Perl will reuse memory it's no longer using, it will generally not release it. It's not SOAP, it's Perl.

Re: Does SOAP clean memory?
by cmilfo (Hermit) on Feb 08, 2002 at 21:47 UTC
    Thank you for the comments. I've been doing some more reading on the subject. There is a very good message board on SOAP::Lite at Yahoo! Groups and the <a href="http://www.soaplite.com"SOAP::Lite Home Page.