Heap fragmentation prevention on Win32

Minor issues with the Ogre API that can be trivial to fix
User avatar
so0os
Bugbear
Posts: 833
Joined: Thu Apr 15, 2010 7:42 am
Location: Poznan, Poland
x 33

Heap fragmentation prevention on Win32

Post 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.
Sos Sosowski :)
http://www.sos.gd
User avatar
Zonder
Ogre Magi
Posts: 1172
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Re: Heap fragmentation prevention on Win32

Post 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 :)
There are 10 types of people in the world: Those who understand binary, and those who don't...
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99

Re: Heap fragmentation prevention on Win32

Post by Wolfmanfx »

This could also be done through a compiler switch http://msdn.microsoft.com/en-us/library ... 71%29.aspx
User avatar
so0os
Bugbear
Posts: 833
Joined: Thu Apr 15, 2010 7:42 am
Location: Poznan, Poland
x 33

Re: Heap fragmentation prevention on Win32

Post 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
Sos Sosowski :)
http://www.sos.gd
User avatar
Zonder
Ogre Magi
Posts: 1172
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Re: Heap fragmentation prevention on Win32

Post 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.
There are 10 types of people in the world: Those who understand binary, and those who don't...