Re: [xml] Memory leak / cleanup missing after node creation.




I see! So in effect xmlBufferCreateSize does indeed allocate the amount of memory I specify (my xml output buffers never go beyond 4K, so I allocate 8192 just in case). In my app I keep the buffers persistent throughout threads, so I don't constantly create/destroy buffers, just re-use them on the next pass. Seeing as I have to use xmlBufferEmpty or xmlBufferShrink after each use, am I right in thinking that xmlBufferShrink is more optimal since it doesn't clear the data (I am guessing here)?

By the way, I have to reiterate Igor that you have been of invaluable help and knowledge on these matters and I send my thanks again.

Regards,
                Jose.

On 25 May 2004, at 1:04 pm, Igor Zlatkovic wrote:

On 25.05.2004 04:00, Jose Commins wrote:
Thanks again Igor! During the process I also found some more memory oddness - the following below doesn't register as a leak using 'leaks' in FreeBSD Terminal, but I see the memory go up:
>
In my (multithreaded) app I have x amount of buffers allocated with:
    theFinalXML = xmlBufferCreateSize(8192);
If, for example, I perform a 'xmlBufferLength(theFinalXML)' I get back zero. Er, OK, weird, since I thought 'xmlBufferCreateSize' can pre-allocate a known size so the buffer doesn't need to be constantly re-malloc'ed (or whatever scheme it uses).
    So if I dump the buffer (this is in my main thread loop):
theFinalXMLSize = xmlNodeDump(theFinalXML, doc, root_element, 0, 0);
    I *have* to perform:
    xmlBufferEmpty(theFinalXML);
afterwards, otherwise the buffers grow and grow. Um, I am fine with using BufferEmpty (apart from the performace hit of constant allocs and reallocs), but then what use has 'xmlBufferCreateSize' ?

No reallocation has happened unless your nodes, in the serialised form, were bigger than 8k. More, xmlBufferEmpty does not free any memory, it just erases the content, so it cannot be that calling this function makes your buffer memory smaller.

xmlBufferCreateSize creates a buffer with the specified initial size and allocates its memory. xmlBufferLength returns the length of the content. After you created the buffer, you haven't written any data in it yet, so the content length is 0. But the memory of the specified size has been allocated. If you write no more than the initial size into the buffer, no reallocation will occur.

When you write more data into a buffer than it can hold, the buffer will grow automatically. It will however never shrink automatically, that you must tell it to do explicitly (xmlBufferShrink) but this operation probably does not do what you think it does.

Ciao,
Igor





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]