Sign in to follow this  
derek_of_bodom@hotmail.com

My program seems to have a memory leak, but there's absolutely no explanation.

Recommended Posts

I'm trying to make a game, but while looking at the Task manager, I discovered that the memory for my program was increasing with no apparent cause. I decided to stip out any code that might be allocating anything, and I STILL have the memory increasing.

Here's the loop that seems to be what is causing the leak. I can't say it's really a memory leak, but I can't think of any other reason why the memory would continuously increase.

[CODE]
while(!done) // Loop That Runs While done=FALSE
{
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
done = true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active && !Tick()/*Replace this with Tick()*/) // Active? Was There A Quit Received?
{
done=true; // ESC or DrawGLScene Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}
[/CODE]

The code for "Tick()" is here:

[CODE]
int Tick()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return 1;
}
[/CODE]

I just copied some of the code from one of NeHe's tutorials.

Is it possible that I have a memory leak, or is it normal for the memory to do this?

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1329976502' post='4915784']
How much is it increasing by? Because the code you posted certainly isn't leaking memory.
[/quote]

In task manager, it goes from "5364 K" to "5368 K" in about 10 seconds, then it continues increasing at that rate, I haven't kept it running for long enough to see how high it gets, but I've let it get as high as "6000 K" and it was still going. I don't know if it's normal for the memory to do that, but it's never done that before. I would post all the source code, but I know that nothing else could be causing the problem because everything else is only called, right before that loop.

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='Nyxenon' timestamp='1329976766' post='4915785']
[quote name='Cornstalks' timestamp='1329976502' post='4915784']
How much is it increasing by? Because the code you posted certainly isn't leaking memory.
[/quote]

In task manager, it goes from "5364 K" to "5368 K" in about 10 seconds, then it continues increasing at that rate, I haven't kept it running for long enough to see how high it gets, but I've let it get as high as "6000 K" and it was still going. I don't know if it's normal for the memory to do that, but it's never done that before. I would post all the source code, but I know that nothing else could be causing the problem because everything else is only called, right before that loop.
[/quote]
Meh, I wouldn't worry about it. An increase of 2K over a period of 10 seconds isn't much to worry about. The thing is, the task manager isn't very good for finding and detecting memory leaks. It shoes how much memory the OS has had to set aside for your application to run, which may not accurately represent how much memory you've allocated yourself (i.e. you've allocated a few megabytes, but then the OS has to use an additional extra few megabytes (and this may fluctuate) to manage your program).

If I were you, I'd just make sure to be using smart pointers where possible, and also get familiar at some point with some memory leak detector at some point (there are different ones that can be used depending on your compiler/IDE that can give you some nice reports of what memory leaked).

Share this post


Link to post
Share on other sites
mhagain    13430
That's not a memory leak, that's normal operation of OpenGL: [url="http://www.opengl.org/wiki/FAQ#Memory_Usage"]http://www.opengl.or...AQ#Memory_Usage[/url]

To be honest, worrying about memory leaks and monitoring memory usage at this early stage in a program is incredibly premature. Wait till you start creating some objects and allocating some memory yourself. Even then it's too early to worry about it.

The best memory leak detector is reading documentation and understanding how things are supposed to work. False positives like this are much more dangerous and time-consuming than tracking a real leak.

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1329978126' post='4915791']
That's not a memory leak, that's normal operation of OpenGL: [url="http://www.opengl.org/wiki/FAQ#Memory_Usage"]http://www.opengl.or...AQ#Memory_Usage[/url]

To be honest, worrying about memory leaks and monitoring memory usage at this early stage in a program is incredibly premature. Wait till you start creating some objects and allocating some memory yourself. Even then it's too early to worry about it.

The best memory leak detector is reading documentation and understanding how things are supposed to work. False positives like this are much more dangerous and time-consuming than tracking a real leak.
[/quote]

I'm not exactly early, the code I provided was backed up code because I thought I had a memory leak with a list class that I wrote. Thanks for the help, Cornstalks and mhagain.

Share this post


Link to post
Share on other sites
Antheus    2409
Task manager doesn't report useful memory usage. It reports number of memory pages requested by process, which is only loosely correlated with leaks.

Or vice versa - a leak will cause number of pages to increase, but it's not the only reason for that number to increase.

[quote]I thought I had a memory leak with a list class that I wrote[/quote]

That's quite possible, but it's not the code that was posted above.

Share this post


Link to post
Share on other sites
iMalc    2466
"Task Manager", oh that anchient piece of crap. Every self-respecting developer on Windows uses Process Explorer from Sysinternals.
Heck it's the first thing I install after the OS, I kid you not.
Once you're tried it, you'll never go back!

Share this post


Link to post
Share on other sites
NightCreature83    5002
[quote name='iMalc' timestamp='1330021511' post='4915931']
"Task Manager", oh that anchient piece of crap. Every self-respecting developer on Windows uses Process Explorer from Sysinternals.
Heck it's the first thing I install after the OS, I kid you not.
Once you're tried it, you'll never go back!
[/quote]
You
[quote name='Nyxenon' timestamp='1329978912' post='4915794']
[quote name='mhagain' timestamp='1329978126' post='4915791']
That's not a memory leak, that's normal operation of OpenGL: [url="http://www.opengl.org/wiki/FAQ#Memory_Usage"]http://www.opengl.or...AQ#Memory_Usage[/url]

To be honest, worrying about memory leaks and monitoring memory usage at this early stage in a program is incredibly premature. Wait till you start creating some objects and allocating some memory yourself. Even then it's too early to worry about it.

The best memory leak detector is reading documentation and understanding how things are supposed to work. False positives like this are much more dangerous and time-consuming than tracking a real leak.
[/quote]

I'm not exactly early, the code I provided was backed up code because I thought I had a memory leak with a list class that I wrote. Thanks for the help, Cornstalks and mhagain.
[/quote]

Have a look at Visual Leak Detector as a memory leak detector, it gives you callstacks for where the allocations have been made. This will only work if you arent using your own memory manager, it hooks into the VC runtime allocator to figure everything out, but that is true for most leak detectors. You just have to include one header in the main cpp of your app and link against the provided lib file, as a nice added benefit you don't have to remove the header declaration for release builds the header takes care of not being compiled with the code in release.

Share this post


Link to post
Share on other sites
Potatoman    108
I can't remember the exact syntax, but there are _Crt methods to dump all the allocations since a certain checkpoint. I'm not sure I'd trust task manager to provide the information I need.

_CrtDumpMemoryLeaks is one to try, for picking up true leaks - but it's also possible some code is growing an array (which gets cleaned up on shutdown), which is just as bad. So in this case use the _CrtMemCheckpoint methods to see get a list of all new heap allocations since the last call.

See this page for details http://www.codeguru.com/forum/showthread.php?t=312742

Share this post


Link to post
Share on other sites
bubu LV    1436
[quote name='iMalc' timestamp='1330021511' post='4915931']Every self-respecting developer on Windows uses Process Explorer from Sysinternals.[/quote]
Or [url="http://processhacker.sourceforge.net/"]ProcessHacker[/url] that does the same things and much more. And it's open-source!

Share this post


Link to post
Share on other sites
iMalc    2466
[quote name='Martins Mozeiko' timestamp='1330151904' post='4916438']
[quote name='iMalc' timestamp='1330021511' post='4915931']Every self-respecting developer on Windows uses Process Explorer from Sysinternals.[/quote]
Or [url="http://processhacker.sourceforge.net/"]ProcessHacker[/url] that does the same things and much more. And it's open-source!
[/quote]Thanks, I didn't know about that one!

It looks pretty good, but alas as well as having more in some places it also has slightly less in others.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this