Skip to main content
GameDev.net gamedev.net
🔒 Locked

Too much memory!

Started by BradDaBug Dec 17, 2002 at 2:35 PM 12 replies 700+ views
Original Post
BradDaBug
BradDaBug
My game is using WAY too much memory. 150 MB, to be exact. Its just a simple side scrolling space shooter. I''ve figured up all the stuff it loads, and its only about 12 MB. The in-game stuff that it uses (the aliens, missiles, etc) it creates is only a few KB. What could be sucking up all that memory! I''ve checked the game for memory leaks (via Valgrind), and it''s only leaking a few bytes!
I like the DARK layout!
niyaw
niyaw
two-dimensional arrays of stuff?

and "a few bytes" isn''t as good as you think. i suggest you turn that figure into 0 before continuing.
Stary
Stary
quote:
Original post by BradDaBug
My game is using WAY too much memory. 150 MB, to be exact. Its just a simple side scrolling space shooter. I've figured up all the stuff it loads, and its only about 12 MB. The in-game stuff that it uses (the aliens, missiles, etc) it creates is only a few KB.

What could be sucking up all that memory! I've checked the game for memory leaks (via Valgrind), and it's only leaking a few bytes!


With this limited information, I'd say you're probably using classes and copying the class instead of using references or pointers. Like:


    class C {
public:
int contents[100];
};

int somefunction(C myC);


// later in some other function...


C c1;
// do stuff with c1.


if (somefunction(c1) {
// etc

}


If somefunction was actually a member function that stored myC somewhere in an object, you'd now have two copies of the contents array. Do this with a few big textures and you'll eat memory real quick.

Of course I may be dead wrong since you hardly give any information enough to analyze the issue.

[edited by - Stary on December 17, 2002 4:15:52 PM]
BradDaBug
BradDaBug
I''m not sure how I''d give more info without posting all the code to my game, but I''ll look over it some more and try to figure out what''s going on.

Well, here''s some more info: I''m using OpenGL for 2D style orthogonal (or is it orthographic?) blitting, and SDL. It SHOULD be just loading a bitmap or whatever onto an SDL surface, then create an OpenGL texture from it, then delete the SDL surface.
I like the DARK layout!
BradDaBug
BradDaBug
WOAH!

I wrote a quick program using the GUI i wrote that the other game (the one using 150 MB) uses, and I tested it using SDL instead of OpenGL. No memory problems. But when I tell it to use OpenGL to blit instead of SDL, guess what? 150 MB!

I''ve also tested whether or not loading something changes how much memory is used. Not really. It uses about 140 MB when I don''t load anything, 150 MB when I load a few big bitmaps.

So its something to do with the GUI thigy''s OpenGL init code.
I like the DARK layout!
Xanth
Xanth
You should probably post the code you think may be the problem, it may help someone trying to help you.
"I thought Genius lived in bottles..." - Patrick Star
BradDaBug
BradDaBug
Here''s the offending (i think) code:


  
GUI_ERROR GUI_InitGraphics(int width, int height, int depth, bool fullscreen, bool usegl)
{
Uint32 flags = 0;

ScreenWidth = width;
ScreenHeight = height;
ScreenDepth = depth;

if (usegl)
{

WriteLog("Setting atribute...");
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
lpBackBuffer = new GUI_Surface;
WriteLog("Settings mode...");

if (fullscreen)
flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_FULLSCREEN;

else
flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE;

lpBackBuffer->surface = SDL_SetVideoMode ( width , height,depth, flags ) ;

if (lpBackBuffer->surface == NULL)
{
WriteLog("Unable to set video mode! %s", SDL_GetError());
}

/* Enable smooth shading */
glShadeModel( GL_SMOOTH );
glEnable(GL_TEXTURE_2D);

/* Set the background black */


glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f,width,height,0.0f,-1.0f,1.0f);
glMatrixMode(GL_MODELVIEW);

//Turn on smooth texturing

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

WriteLog("Loading surfaces...");
WidgetSurface= GUI_LoadSurface("interface.bmp");
BlackFontSurface = GUI_LoadSurface("font.bmp");
WhiteFontSurface = GUI_LoadSurface("font_white.bmp");
RedFontSurface = GUI_LoadSurface("font_red.bmp");

GrayFontSurface = GUI_LoadSurface("font_gray.bmp");

ScalingFactor = 1.0f;
}

else
{
//Do a bunch of SDL only stuff

}



WriteLog("Loading surfaces...");
WidgetSurface= GUI_LoadSurface("interface.bmp");
BlackFontSurface = GUI_LoadSurface("font.bmp");
WhiteFontSurface = GUI_LoadSurface("font_white.bmp");
RedFontSurface = GUI_LoadSurface("font_red.bmp");


if (lpBackBuffer == NULL)
{
WriteLog("Unable to set video mode!");
return(GUIERR_GENERIC);
}

States.CanDraw = true;
char drivername[20];
SDL_VideoDriverName(drivername, 20);
WriteLog("Graphics initialized! Using driver %s", drivername);

return(GUI_OK);
}


Its pretty much a cut and paste of the GL init code. I also guess I should include GUI_LoadSurface(), so here it is:


  
GUI_Surface *GUI_LoadSurface(char *pic)

{


WriteLog("Loading %s...", pic);


GUI_Surface *tmpsurface = new GUI_Surface;

SDL_Surface *surface = NULL;

SDL_Surface *alphasurface;





surface = IMG_Load(pic);





if (surface == NULL)
{
delete tmpsurface;

return(NULL);
}



SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, 255,0,255));





tmpsurface->w = surface->w;

tmpsurface->h = surface->h;




if (UsingGL)

{



alphasurface = SDL_DisplayFormatAlpha(surface);


if (alphasurface == NULL)
{
WriteLog("Failed to load surface! %s", SDL_GetError());
delete tmpsurface;
return(NULL);
}
tmpsurface->surface = alphasurface;


glGenTextures( 1, &tmpsurface->gltex);


// Typical Texture Generation Using Data From The Bitmap

glBindTexture( GL_TEXTURE_2D, tmpsurface->gltex);


//Generate The Texture

glTexImage2D( GL_TEXTURE_2D, 0, 4, surface->w,
surface->h, 0, GL_BGRA_EXT,
GL_UNSIGNED_BYTE, alphasurface->pixels );

// glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w,

// surface->h, 0, GL_BGR,

// GL_UNSIGNED_BYTE, alphasurface->pixels );



//Linear Filtering


glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );



SDL_FreeSurface(surface);


tmpsurface->surface = alphasurface;

tmpsurface->alpha = 255;



SDL_FreeSurface(tmpsurface->surface);

tmpsurface->surface = NULL;



return(tmpsurface);

}



tmpsurface->surface = surface;



return(tmpsurface);

}






[plug] If all this looks really ugly, then you can look at it in CVS over at SourceForge! http://www.sf.net/projects/libgui[/plug]

Ah nuts. I just noticed one problem. Its loading those surfaces twice.
I like the DARK layout!
BradDaBug
BradDaBug
OK, I have continued to try to figure out what is causing the problem, and I have determined that the problem apparently is not mine. This tiny little bit of code is using 135 MB of ram:

  
#include <stdio.h>
#include <SDL/SDL.h>

int main()
{
Uint32 flags = 0;
SDL_Surface *surface;
printf("Hello world\n");
SDL_Init(SDL_INIT_VIDEO);

flags = SDL_OPENGL;
surface = SDL_SetVideoMode ( 640 , 480,16, flags ) ;

while(1)
{
//do nothing

}
}


Just a simple SDL program that starts up OpenGL. I checked out Chromium BSU, and its using 140 MB of ram. Turns out it uses SDL and OpenGL too.

Is there a reason SDL/OpenGL uses so much RAM, or is there a bug out there somewhere that oughta be fixed?
I like the DARK layout!
spiffgq
spiffgq
quote:
Original post by BradDaBug
Is there a reason SDL/OpenGL uses so much RAM, or is there a bug out there somewhere that oughta be fixed?

Maybe OpenGL is using mipmaps.
SpiffGQ
athos_musketeer
athos_musketeer
What program are you using to measure the memory used by your program?
d000hg
d000hg
Dunno if it''s relevant but does SDL support multisampling in the context you''re using it? If you were using the highest level for example:
640x480 in 16bit colour is ~0.5Mb. If you were using D3DMULTISAMPLE_16_SAMPLES I''m not sure if that means you mutiply the offscreen buffer by 16 in each direction. If so that makes your memory needed 256 times bigger.

640x480x2x256=157Mb.
Doubt this is the cause but who knows




Read about my game, project #1
NEW (18th December)2 new screenshots, one from the engine and one from the level editor



John 3:16
BradDaBug
BradDaBug
I posted this problem on the SDL mailing list, and the SDL guy (Sam Lantinga) said this:

WTF!? I can''t paste between Ximian and Mozilla?!

OK, to paraphrase what he said, Linux consideres all the VRAM on my video card as memory that is being used, so that''s why its saying 135 MB of RAM is being used.

I thought it was odd that even when the game was running, the memory usage graph would barely go up, despite my game supposedly using SO much ram.
I like the DARK layout!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.