Menu

#33 Memory fragmention every frame (new/delete[] in Draw/UpdateS

open
nobody
None
5
2004-05-04
2004-05-04
Anonymous
No

I know no one seems to be supporting this anymore, but
thought others would appreciate a fix to
framerate/memory fragmentation that occurs with the
current code base.

In KrEngine::UpdateScreen(), if you remove the
new/delete[] pair and put in a simple growing array
system, this gets rid of one new delete every frame, like
so:

if( rectArray->Count() >
nNumSDLRects)
{
delete [] sdlRects;

//resize the array, to next
multiple of 8
nNumSDLRects = rectArray-
>Count() + (8 - (rectArray->Count() % 8));
sdlRects = new SDL_Rect
[nNumSDLRects];
memset(sdlRects, 0, sizeof
(SDL_Rect) * nNumSDLRects);
}

Also, in KrEngine::Draw() if you pass in your own array,
set to an average size to start (not zero), and change
GlDynArray<> to have a Reset() (for count only, not
size) as well as a Clear(), then you can get rid of
multiple new/delete[]s every frame by changing this
function to setup rectArray this way:

GlDynArray< KrRect >* rectArray = (
_rectangles ) ? _rectangles : &rectArrayOnStack;
rectArray->Reset();

Enjoy...
Tim

Discussion


Log in to post a comment.