Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy deleted memory is unavailable to use? C++
    primarykey
    data
    text
    <p>I have a <strong>200 MB</strong> XML file that I am loading using <em>TinyXML</em>. My problem is that when the TinyXML object is destroyed, the memory it used will not be reused. I have used a memory leak detector that I have used in other projects, and have manually stepped through the code and have not been able to find any memory leaks, so I don't suspect that leaked memory is the problem.</p> <p>This code will reproduce the problem:</p> <pre><code>#include &lt;iostream&gt; #include &lt;tinyxml.h&gt; int main() { char* filename = "../LargeFile.xml"; { TiXmlDocument targetDoc( filename ); targetDoc.LoadFile(); } char* buf = new char[ 524288000 ]; delete [] buf; return 0; } </code></pre> <p>Using <a href="http://hashpling.org/asm/" rel="nofollow noreferrer">Address Space Monitor</a> I can see that after <code>LoadFile()</code> there is a large chunk of red, then after <code>targetDoc</code> is destroyed it is all yellow. Then when the final char buf is allocated it appears in red but over the top of green space rather than the yellow freed from TinyXML. If try to allocate more buffers than there is green address space the application will crash(out of memory). This can be seen in the images below.</p> <p><img src="https://i.stack.imgur.com/gUAya.png" alt="After Load"> <img src="https://i.stack.imgur.com/VLCiT.png" alt="After Unload"> <img src="https://i.stack.imgur.com/BxZYj.png" alt="Char Buffer Allocation"></p> <p>According to the Address space monitor page <em>"Free address space is shown in green, reserved addresses in yellow and used (committed) memory regions in red"</em> So why is the memory that is freed by TinyXML staying <em>"reserved"</em> according to Address Space Monitor. What can cause that to happen, and how do I stop it?</p> <p><strong>EDIT:</strong> </p> <p><em>"Are you allocating large buffers? If so, then there may be memory fragmentation in the yellow space and there's nowhere that a large contiguous buffer can be allocated"</em></p> <p>Great question however this would indicate that TinyXML has a memory leak and none of my tools have shown there to be one.</p> <p><strong>UPDATE</strong></p> <p>I made loop that endlessly allocated ints, which eventually appeared to used all the yellow space. However larger allocations don't use it. This indicated to me that there were small leaks that were getting littered through the heap during the parsing of the xml file by tinyXML which fragments the heap in such a way that only objects small enough to fit between the leaks could be allocated in the yellow space. So I searched longer and harder for leaks in TinyXML and still did not find any. Everything seems to be getting freed up correctly, which brings me back to my initial state of confusion.</p> <p>I am at a loss to explain the problem.</p> <p><img src="https://i.stack.imgur.com/BxZYj.png" alt="Char Buffer Allocation"> <img src="https://i.stack.imgur.com/yDmll.png" alt="Char Buffer Allocation2"> <img src="https://i.stack.imgur.com/FtkIW.png" alt="Char Buffer Allocation3"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload