Sign in to follow this  

Mem usages rises and falls

This topic is 3374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm making an application with some directX components and some windows components. The problem is that when i just open the application the Mem Usage in the Task Manager reads something like 37,000 K then it keeps rising and rising and the suddenly falls to something like 1,190 K. Anyone has any idea on why that happens.

Share this post


Link to post
Share on other sites
At a wild guess, I'd say because something in your program allocates around 36MB of memory, and then frees it.

Given that we know nothing about your program, I don't know if you were expecting more detailed answers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
At a wild guess, I'd say because something in your program allocates around 36MB of memory, and then frees it.

Given that we know nothing about your program, I don't know if you were expecting more detailed answers.


Right, sorry for the vague question, the app is pretty simple and it all fits within about 100 lines but I'm using custom functions which span into thousands of lines, so posting code wouldn't be that usefull.
Any ways by the means of good ol' comment-stuff-out method I managed to get the program to load at 13K and just sit there, which is fine. I have a couple of questions though about the whole memory management thing. Is there any way I can monitor how much memory my program allocates, especially if I'm dealing with another API (So for example I have no idea what the DirectX functions I'm calling are allocating in the background). Another thing I've been thinking about is garbage collection. I have a couple of really good books that tell me how to implement this, and there is a helpful resouce on this site as well. Do you think it is necessary in game development, and do it's benefits outweigh it's compromises (precisely, the overhead for collecting all the loose pointers). What do you guys think?

Share this post


Link to post
Share on other sites
I think garbage collection in C++ is such a pain, and so tricky to get right, that you're better off without it. Unless, perhaps, you're dealing with legacy code which leaks memory left and right.

However, I'm not sure I see why it's such a problem that the functions you call allocate memory?

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
I think garbage collection in C++ is such a pain, and so tricky to get right, that you're better off without it. Unless, perhaps, you're dealing with legacy code which leaks memory left and right.

However, I'm not sure I see why it's such a problem that the functions you call allocate memory?


Thats no problem at all, but since the program started at 30,000K and over a minute escalated to 100,000K then i figured i was calling "new" in some loop. (And so I was, I was rebuilding a string without releasing the previous memory). I do agree that a garbage collector is no easy task to achieve, and I'm not really that enthusiastic as to start writing one, but like I said previously, I want to know exactly how much memory my app allocated. Wouldn't the GC allow me to do that? I can modify it to where it wont collect garbage but just monitor how much memory is currently used. Or is there a better approach to it? Thanks

Share this post


Link to post
Share on other sites
Instead of garbage collection, you might want to look into tracking your allocations. It's not too difficult to overload global operator new and track them. If you're using Visual Studio then VLD will do it for you, it'll even tell you the call stack for each leak.

Share this post


Link to post
Share on other sites
Quote:
Original post by VanillaSnake21
Hi, I'm making an application with some directX components and some windows components. The problem is that when i just open the application the Mem Usage in the Task Manager reads something like 37,000 K then it keeps rising and rising and the suddenly falls to something like 1,190 K. Anyone has any idea on why that happens.
Task Manager isn't at all accurate, particularly when using DirectX stuff. The "Mem Usage" doesn't mean what you might think, it's the processes working set size.

Share this post


Link to post
Share on other sites
DevFred
Quote:

By "string", do you mean a char* or a std::string?

char*

Adam_42
Quote:

Instead of garbage collection, you might want to look into tracking your allocations. It's not too difficult to overload global operator new and track them. If you're using Visual Studio then VLD will do it for you, it'll even tell you the call stack for each leak.

This is pretty impressive, I didn't even know that such a plugin existed, Thanks, I'll definitely try it out.

@Antheus & Evil Steve
I know TM isn't the best way to track memory allocation, but it lets me know if theres a significan leak somewhere (like what I had before, with the new in a loop).

Thanks for the replies everyone






Share this post


Link to post
Share on other sites
I'm surprised no one have mentioned this yet, but *why* are you calling new in a loop? In C++ you should basically never allocate memory with new or malloc in user code. When creating an object, create it on the stack, and let the object itself allocate the memory it needs, and frees it in the destructor. That way, when the stack object goes out of scope, your memory is automatically freed, and you won't leak memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
I'm surprised no one have mentioned this yet, but *why* are you calling new in a loop? In C++ you should basically never allocate memory with new or malloc in user code. When creating an object, create it on the stack, and let the object itself allocate the memory it needs, and frees it in the destructor. That way, when the stack object goes out of scope, your memory is automatically freed, and you won't leak memory.


Well the memory is allocated within the object. Heres a snippet of source


int DXSL_Text::CreateText(LPDIRECT3DDEVICE9 device,
LPCSTR String,
LPCTSTR pFaceName,
int Height,
int Weight,
BOOL Italic,
int Width,
UINT MipLevels,
DWORD CharSet,
DWORD OutPrecision,
DWORD Quality,
DWORD PitchAndFamily
)
{

//USED TO BE
/*
if(!String)
return 0;

m_String = new char[strlen(String)];
memcopy(m_String, String, strlen(String)*sizeof(char));

*/


if(String)
{
if(!SetString(String))
return 0;
}

if(!pFaceName)
return 0;

if(FAILED(D3DXCreateFont(device, Height, Width, Weight, MipLevels, Italic, CharSet, OutPrecision, Quality, PitchAndFamily, pFaceName, &m_Font)))
return 0;

return 1;
}



This function itself was called in the main game while() loop in order to update FPS. I changed it so now I call this function during intialization. And only call SetText(...) to draw the FPS in the main loop.

Share this post


Link to post
Share on other sites

This topic is 3374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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