Page 1 of 1

Heap fragmentation prevention on Win32

Posted: Fri Jun 24, 2011 12:29 pm
by so0os
I recently ran into a heap fragmentation issue.
Ogre failed to allocate around 18MB whereas the WorkingSetSize returned was around 1GB. In my app user travels between fixed locations a lot causing them to load and unload tons of objects, each if which is wrapped in a fairly large number Ogre3D class instances. I have been suggested to use this after each delete:

Code: Select all

SetProcessWorkingSetSize( GetCurrentProcess(), -1, -1 );
And when set after inbetween-scene reloads, it effectively got rid of the problem.
My point is to extend OGRE_FREE, OGRE_DELETE_T, OGRE_DELETE_ARRAY_T, OGRE_DELETE, OGRE_DELETE_ARRAY_T_ALIGN, OGRE_DELETE_T_ALIGN, OGRE_DELETE_ARRAY_T_SIMD and OGRE_DELETE_T_SIMD macros with calls to this function to prevent this happening OR include that in ResourceManagers' unload* methods. It's not much of a work and can save some head scratching in particular circumstances.

So, my final question is, do you think it's a good idea, or does the user need to do that kind of stuff himself.

Re: Heap fragmentation prevention on Win32

Posted: Fri Jun 24, 2011 12:59 pm
by Zonder
Calling this in OGRE itself should not be done as it needs to be called in a controlled manner.

You need to control when calling it as you can cause all your mem to be flush to the paging file which then has to be loaded back into RAM as your app then continues executing. As you have found calling it when you have done massive clean up can help fix fragmentation but it isn't a solution all the time.

One way to help prevent your original issue happening is object reuse. Have a method clean up the internal data structures to their initialised defaults. Of couse this could still be deallocating RAM but you arn't deallocating as much (also it should increate performance as not as much ram is been deallocated). Of couse using this method isn't straight forward essentialy you'd be implementing a garbage collector type system :)

Re: Heap fragmentation prevention on Win32

Posted: Fri Jun 24, 2011 1:01 pm
by Wolfmanfx
This could also be done through a compiler switch http://msdn.microsoft.com/en-us/library ... 71%29.aspx

Re: Heap fragmentation prevention on Win32

Posted: Fri Jun 24, 2011 1:08 pm
by so0os
Wolfmanfx wrote:This could also be done through a compiler switch http://msdn.microsoft.com/en-us/library ... 71%29.aspx
Wow, good point, i suppose it renders the issue obsolete

Re: Heap fragmentation prevention on Win32

Posted: Fri Jun 24, 2011 1:18 pm
by Zonder
so0os wrote:
Wolfmanfx wrote:This could also be done through a compiler switch http://msdn.microsoft.com/en-us/library ... 71%29.aspx
Wow, good point, i suppose it renders the issue obsolete
This may not function due the app not going idle.