How to check if code's leakin, how much mem takes etc..

Discussion about everything. New games, 3d math, development tips...
Post Reply
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

How to check if code's leakin, how much mem takes etc..

Post by MasterGod »

I've seen some places where people were doing something with _CRT...
I'm not sure if it has anything to do with what I ask but if it does, how can I check how much memory my app takes and check for performance stuff like that programmatically?

Thanks.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I don't know about programmatically, i'm sure there's something in the windows API that will allow you to see how much memory is free.

But you could just look at the task manager and see if the memory usage for your app keeps on rising when it shouldn't or is just generally high.

An interesting note on PS3 programming, there's a function to return how much memory is available and how much the total memory is but free and delete don't replenish the value for how much memory is available so it's impossible to tell without wrapping a counter around malloc, free, new and delete! And i've been told by the core developers that they have no plans on fixing this 'bug' as big games companies always have their own pool of memory which they keep an eye on and so never really use the functions for how much memory is free.
Image Image Image
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

JP wrote:I don't know about programmatically, i'm sure there's something in the windows API that will allow you to see how much memory is free.

But you could just look at the task manager and see if the memory usage for your app keeps on rising when it shouldn't or is just generally high.

An interesting note on PS3 programming, there's a function to return how much memory is available and how much the total memory is but free and delete don't replenish the value for how much memory is available so it's impossible to tell without wrapping a counter around malloc, free, new and delete! And i've been told by the core developers that they have no plans on fixing this 'bug' as big games companies always have their own pool of memory which they keep an eye on and so never really use the functions for how much memory is free.
Don't want to hijack the thread but.. what about us poor Linux programmers? how do we check it? :oops:
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Valgrind is a superb Linux-only memory checker. hybrid uses it, so may be able to advise you on using it.

Fortify is another free alternative; not as powerful as Valgrind, but cross platform.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Thanks guys but I don't want a tool :? ..
I want to check and like printf() how much mem my app takes etc..
I'm telling you I've seen somewhere something like that with something like _CRT but I don't remember where nor how to use it..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Just curious, as this is how Insomniac Games did it on Windows, could you not just purge the program handle. I mean Win32 API does provide easy access to all the data you need on your program, MSDN is good for help.

But yeah, if you want to see how they did it, look it up on their R&D, which has grown significantly larger.
TheQuestion = 2B || !2B
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

At a basic level the Processes tab in Task Manager will show stuff like working set. There's a set of 20 or so columns of data to choose from.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

MasterGod wrote:Thanks guys but I don't want a tool :? ..
I want to check and like printf() how much mem my app takes etc..
I'm telling you I've seen somewhere something like that with something like _CRT but I don't remember where nor how to use it..
Uh, for some reason I got the impression that this had already been answered. Sorry. :oops:

GetProcessMemoryInfo()

(Call GetCurrentProcess() to get a handle for the running process).
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

rogerborg wrote:
MasterGod wrote:Thanks guys but I don't want a tool :? ..
I want to check and like printf() how much mem my app takes etc..
I'm telling you I've seen somewhere something like that with something like _CRT but I don't remember where nor how to use it..
Uh, for some reason I got the impression that this had already been answered. Sorry. :oops:

GetProcessMemoryInfo()

(Call GetCurrentProcess() to get a handle for the running process).
Thanks that looks like what I wanted but it still isn't :wink:
I need something with _CRT in its main()..



After a little search I found what I was looking for but I don't know how to use it:
CuteAlien wrote:I did need so long to reproduce this bug that it's getting a little late here. So I just post it and hope someone finds the solution before me ;-)
(otherwise I will search tomorrow myself)

The following code produces a memoryleak:

Code: Select all

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <irrlicht.h>

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

int main()
{

	_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

	struct testrow
	{
		irr::core::array<bool> items; // type in array does not matter
	};

	irr::core::array< testrow > rows;
	testrow row;
	row.items.push_back( true );
	rows.push_back( row );
	rows.insert( row, 0 );

return 0;
}
The 'crt' stuff in the code is just to detect memleaks with VisualStudio.
Adding more items or inserting more rows will produce bigger leaks.
pushing back rows (instead of using insert) won't produce leaks.

Found in an old Irrlicht, but also tested with current svn .
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Only those 4 lines should matter:

Code: Select all

#define _CRTDBG_MAP_ALLOC 
#include <crtdbg.h> 
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 
That's the way the VS Express which I'm using does report memory leaks whenever I quit the application. But maybe just look in the documenation of your version of VS, I'm not sure if it's the same across different versions.

Except that I also recommend the taskmanager (or top in Linux). To get the amount of dynamic memory used you can also overload the new/delete operators and count it yourself, but that can get rather tricky in some cases. And for static memory it gets even more difficult. Also the amount of memory really used might have not a lot in common with the amount of memory which you request. For example doing a new call for a larger array will in any newer PC system not reserve any memory until you actually write into it.

On Linux I also recommend valgrind. By default it will check for memoryleaks (just call: valgrind ./yourapp). But it can also give some information about memory usage (I think it was: valgrind --tool=memcheck ./yourapp).

For windows I don't know similar tools which are free. If you have lots of money I can recommend boundschecker which will slow down the execution of your application very much (so you usually have to script the gameplay you want to test in advance...), but it finds more leaks than any other tools which I used so far.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply