title: Hunting down EOutOfResources
body: <p>Question:</p>
  
  <p>Is there an easy way to get a list of <em>types</em> of resources that leak in a running application?  IOW by connecting to an application ? </p>
  
  <p>I know memproof can do it, but it slows down so much that the application won't even last a minute. Most taskmanager likes can show the number, but not the type.</p>
  
  <p>It is not a problem that the check itself is catastrophic (halts the app process), since I can check with a taskmgr if I'm getting close (or at least I hope)</p>
  
  <p>Any other insights on resource leak hunting (so not memory) is also welcomed.</p>
  
  <p>Background:</p>
  
  <p>I've an Delphi 7/2006/2009 app (compiles with all three) and after about a few week it starts acting funny. However only on one of the places it runs, on several other systems it runs till the power goes out.</p>
  
  <p>I've tried to put in some debug code to narrow the problem down. and found out that the exception is EOutofResources on a save of a file. (the file save can happen thousands of times a day).</p>
  
  <p>I have tried to reason out memory leaks (with fastmm), but since the dataflow is quite high (60MByte/s from gigabit industrial camera's), I can only rule out "creeping" memory leaks with fastmm, not quick flashes of memoryleaks that exhaust memory around the time it happens. If something goes wrong, the app fills memory in under half a minute.,</p>
  
  <p>Main suspects are filehandles that are somehow left on some error and  TMetafiles (which are streamed to these files). Minor suspects are VST, popupmenu and tframes</p>
  
  <p>Updates: </p>
  
  <p>Another possible tip: It ran fine for two years with D7, and now the problems are with Turbo Explorer (which I use for stable projects not converted to D2009 ).</p>
  
  <p>Paul-Jan: Since it only happens once a week (and that can happen at night), information acquisition is slow. Which is why I ask this question, need to combine stuff for when I'm there thursday. In short: no I don't know 100% sure. I intend to bring the entire Systemtools collection to see if I can find something (because then it will be running for days). There is also a chance that I see open files. (maybe should try to find some mingw lsof and schedule it)</p>
  
  <p>But the app sees very little GUI action (it is an machine vision inspection app), except screen refresh +/- 15/s which is tbitmap stretchdraw + tmetafile, but I  get this error when saving to disk (TFileStream) handles are probably <i>really</i> exhausted. However in the same stream, TMetafile is also savetostreamed, something which later apps don't have anymore, and they can run from months.  </p>
  
  <p>------------------- UPDATE</p>
  
  <p>I've searched and searched and searched, and managed to reproduce the problems in-vitro two or three times.  The problems happened when memusage was +/- 256MB (the systems have 2GB), user objects 200, gdi objects 500, not one file more open than expected ).</p>
  
  <p>This is not really exceptional. I do notice that I leak small amounts of handles, probably due to reparenting frames (something in the VCL seems to leak HPalette's), but I suspect the core cause is a different problem. I reuse TMetafile, and .clear it inbetween.  I think clearing the metafile doesn't really (always?) resize the resource, eventually each metafile in the entire pool of tmetafile at maximum size, and with 20-40+ tmetafiles (which can be several 100ks each) this will hit the desktop heap limit. </p>
  
  <p>That's theory, but I'll try to verify this by setting the desktop limit to 10MB at the customers, but it will be several weeks before I have confirmation if this changes anything. This theory also confirms why this machine is special (it's possible that this machine naturally has slightly larger metafiles on average). Occasionally freeing and recreating a tmetafile in the pool might also help.</p>
  
  <p>Luckily all these problems (both tmetafile and reparenting) have already been designed out in newer generations of the apps.</p>
  
  <p>Due to the special circumstances (and the fact that I have very limited test windows), this is going to be a while, but I decided to accept the desktop heap as an example for now (though the GDILeaks stuff was also somewhat useful).</p>
  
  <p>Another thing that the audit revealed GDI-types usage in a thread (though only saving tmetafiles (that weren't used or connected otherwise) to streams.</p>
  
  <p>------------- Update 2.</p>
  
  <p>Increasing the desktop limit only seemed to minorly increase the time till the problem occurred.</p>
  
  <p>Unfortunately, I won't be able to follow up on this further, since the machines were updated to a newer version of the framework that doesn't have the problem.</p>
  
  <p>In summary I can only state what the three core modifications were going from the old to the new framework: </p>
  
  <ul>
  <li>I no longer change screens by reparenting frames. I now work with forms that I hide and show. I changed this since I also had very rare crashes or exceptions (that could be clicked away) due to this. The crashes were all while operating the GUI though, not spontaneously like the main problem</li>
  <li>The routine where the crash happened dealt with TMetafile. TMetafile has been designed out, and replace by a simpler own made format. (basically arrays with Opengl vertices)</li>
  <li>Drawing no longer happened with tbitmap with a tmetafile overlay strechdrawn over it, but using OpenGL.</li>
  </ul>
  
  <p>Of course it could be something else too, that got changed in the rewrite of the above parts, fixing some very nasty detail bug. It would have to be an extremely bad one, since I analysed the above system as much as I could.</p>
  
  <p><strong>Updated nov 2012</strong> after some private mail discussion: In retrospect, the next step would have been adding a counter to the metafiles objects, and simply reinstantiate them every x * 1000 uses or so, and see if that changes anything. If you have similar problems, try to see if you can somewhat regularly destroy and reinitialize long living resources that are dynamically allocated.</p>