Program with MDX and memory

Started by
6 comments, last by Moe 17 years, 6 months ago
Im coding up a small game and i decided it was time to run it while looking at task manager. And i noticed that the memory that uses just slowly goes up and up and up. Ive checked my code and replaced all the things that were like : Object o = new Object(); with declaring Object o in the class then just making it: o = new Object(); But that doesnt seem to stop it from continuously going up. But I've also noticed that whenever i alt+tab and put it in hte back ground, the memory starts sky rocketing at 1Meg/sec and the CPU usage goes up to 50%(its running on core 2 duo). Does anyone know any common things that would be causing this? If so, please let me know. Thank you in advance.
Advertisement
Do you ever delete your objects? If not then you have a memory leak. Are you sure the CPU usage isn't 50% when playing? Unless you manually pause the CPU then your program will take all the CPU it can get, and because it can only use one core that's 50%.
I was under the impression that in C#, you dont delete objects. From what I read, the garbage collector takes it when its not used anymore and also there is no key word 'delete' in c# (not that i know of atleast). Should i be calling the garbage collector?

Now obviously, in the garbage collector wont collect something that is still being referenced, but if i have the o = new object();, then later call o = new object(), is that first instance still being held?

As for the CPU, how would one go about manually pausing it?
There is no delete, but there is myObj.Dispose(). You can slow your application to a crawl by allocating a largish object in memory in a loop or frequently called method without disposing it. Eventually the Garbage collector will run, but it might not be until to late. You can force the garbage collector to run, but that tends to be a bad idea.

As a rule, if you allocate something of non trivial size and you know you are done with it, call .Dispose().
I checked into the .Dispose() option and came the quick realization that i would have to create my own Dipose() function for my Objects that im creating. But still brings me back to not being sure if the memory is being freed or not. I made a .dispose where it would set it to null, but im not sure if that works. Any pointers? Thank you in advance.
I really think this article contains a lot of down-to-earth info on Dispose, what it does, and why to use it:
http://blogs.msdn.com/clyon/archive/2004/09/21/232445.aspx
Gradual memory increases are to be expected with .Net; the garbage collector won't run until it thinks you're close enough to running out of memory to be worth it. (This is why you need to manually dispose of resource-holding objects like DX textures and so on, as garbage collection might not get called for a loooong time.)

The Alt-Tab thing sounds like a cock-up with the way you're handling lost device contexts, but I'm not really up on MDX. You might get a better answer if someone moves this thread to .Net ...
Quote:Original post by ShadowWolf
I really think this article contains a lot of down-to-earth info on Dispose, what it does, and why to use it:
http://blogs.msdn.com/clyon/archive/2004/09/21/232445.aspx

ShadowWolf: Thanks for the link - I found it to be quite informative!

This topic is closed to new replies.

Advertisement