ZFGC

Resources => Coding => Topic started by: Theforeshadower on January 26, 2012, 08:55:59 am

Title: How to find memory leaks?(C# - XNA)
Post by: Theforeshadower on January 26, 2012, 08:55:59 am
I don't want to actually post all of my code.  You may or may not have seen my new project that I have been working on.  Well, I think it may have a small memory leak(if that is the correct term) of some kind.  It's not a big number by my standards, but the point is that it does exist.  I have been checking my code over and over and over.  I have been looking at when anything is created at all in the code.

The memory leak is about 400bytes per 2 to 3 seconds while the game is running after it starts.  The beginning memory usage via Task Manager shows 13k-ish then slowly rises after a few seconds to the rate I previous stated.

Is there anyway in Visual Studio 2010 Professional to debug to see everytime something is created?  I am guessing it is a variable of some type.  Pretty sure it's not an object as here is a basic overlook of object creation at this point:

SpriteManager Game Component creates 1 instance of objPlayer and objSword at the class level.  That's it as far as any objects go.  I have rechecked my update code in all three(clLink and clSword being the classes for the respectable objects) and nothing is creating any new variables during any type of updating that goes on during the game.

The only code I could think of that may be creating a new variable would be during the Draw portion:
Code: [Select]
        #region Drawing Sprite On Screen
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(currentSprite, position, new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
        }
        #endregion

..the new variable being new Rectangle part...
All sprites would/are dependent on a baseSprite abstract class which calls this during a Draw method call, so I am wondering that everytime I call the Draw on both objects of Player and Sword, it creates a new Rectangle variable.

Any thoughts?  I am probably making a mistake(or a few) based upon inexperience but I am trying to improve through my current project.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: Xfixium on January 26, 2012, 02:50:57 pm
Download a memory profiler if you'd like:

http://memprofiler.com/
http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/

Most likely it is due to your own code, or third party code that may use un-managed code incorrectly. The rectangle struct (If from System.Drawing) should not be causing your memory problem.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: TheDarkJay on January 28, 2012, 01:01:37 am
Creating a new Rectangle every time isn't a good way to go about it, since the object does get constructed every frame.

It could be that the .NET runtime isn't seeing any reason to delete the objects until so much RAM is used up. The garbage collection routines of managed languages tend to wait until sufficient amounts of memory is used up, or so much time has passed, before clearing.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: TheShyGuy on February 01, 2012, 11:41:16 am
It has nothing to do with creating a new Rectangle every time, its a struct-ValueType so its allocated on the stack every time and is cleaned up right afterwards. If it were to be a class-RefType every frame, then you have a problem. And then DarkJay would be right about the GC but the leak is somewhere else in your code.

edit:
The problem might not be creation of objects, but you might be making calls to the api that are "expensive" and causes garbage itself.  You should probably look over your code to check if your making any of those types of calls too much in one frame.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: Theforeshadower on February 01, 2012, 02:44:46 pm
I do not think it's a memory leak per say anymore.  The memory that the program uses peaks out at about 19k.  Once it hits just under that, it stops increasing.  Interesting to say the least, can't quite figure it out.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: HedonisticMachine on March 30, 2012, 03:16:06 am
Hi Theforeshadower.

It's possible that your code does not reach a threshold large enough for the Garbage Collector to run until you reach about 19k. This is normal behavior. A bad memory leak would be something that would continue up until the 100k's and more. .NET is a tricky beast. I'm glad you resolved it as not a memory leak.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: GoodGirl on May 09, 2012, 09:40:41 pm
I advise you try to test the code using deleaker. It will show exactly where the leak  :'( is.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: Matador on May 12, 2012, 09:37:03 am
Is it work for Linux?
Title: Re: How to find memory leaks?(C# - XNA)
Post by: GoodGirl on May 14, 2012, 07:18:59 pm
Is it work for Linux?
I do not know exactly. I am working on windows.
Title: Re: How to find memory leaks?(C# - XNA)
Post by: GoodGirl on May 15, 2012, 04:34:56 pm
... but you can use Wine :))) 8)

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved