Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: How to find memory leaks?(C# - XNA)  (Read 6432 times)

0 Members and 1 Guest are viewing this topic.
How to find memory leaks?(C# - XNA)
« on: January 26, 2012, 08:55:59 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
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.
Logged
  • Super Fan Gamers!
Re: How to find memory leaks?(C# - XNA)
« Reply #1 on: January 26, 2012, 02:50:57 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Posts: 728
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.
Logged
  • Pyxosoft
Re: How to find memory leaks?(C# - XNA)
« Reply #2 on: January 28, 2012, 01:01:37 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
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.
Logged
Re: How to find memory leaks?(C# - XNA)
« Reply #3 on: February 01, 2012, 11:41:16 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 4
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.
« Last Edit: February 01, 2012, 12:14:40 pm by TheShyGuy »
Logged
Re: How to find memory leaks?(C# - XNA)
« Reply #4 on: February 01, 2012, 02:44:46 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
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.
Logged
  • Super Fan Gamers!
Re: How to find memory leaks?(C# - XNA)
« Reply #5 on: March 30, 2012, 03:16:06 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 6
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.
Logged
Re: How to find memory leaks?(C# - XNA)
« Reply #6 on: May 09, 2012, 09:40:41 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 4
I advise you try to test the code using deleaker. It will show exactly where the leak  :'( is.
Logged
Re: How to find memory leaks?(C# - XNA)
« Reply #7 on: May 12, 2012, 09:37:03 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2
Is it work for Linux?
Logged
Re: How to find memory leaks?(C# - XNA)
« Reply #8 on: May 14, 2012, 07:18:59 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 4
Is it work for Linux?
I do not know exactly. I am working on windows.
Logged
Re: How to find memory leaks?(C# - XNA)
« Reply #9 on: May 15, 2012, 04:34:56 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 4
... but you can use Wine :))) 8)
Logged
Pages: [1]   Go Up

 


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



Page created in 0.041 seconds with 57 queries.