Solved

info about deleting objects

Posted on 2006-07-22
4
301 Views
Last Modified: 2010-04-16
i have a near infinite loop (only exits on command).

what im noticing is the longer i run the program, the longer it takes to shut down.  which to me leads me to believe i have a memory leak in my program.  so ive placed the problem to one function.  in this function i create a new instance of Rectangle, and a new instance of Vector3 everytime it is ran (which is approx once every 16.66 milliseconds).

now i was under the assumpsion that after the function executed, these values would be out of scope and therefore cleaned up by the garbage collector.  but it seems not, becuase like i said, if its ran for a couple minutes the program seriously laggs.

i also should note, which i think is an important point, that i am not creating variables with the calls to new Rectange and new Vector3, i am passing them into a function.

i guess my question is now do i delete the references to these objects?

~b
0
Comment
Question by:bramsquad
  • 2
4 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17161835
Hi bramsquad;

The garbage collector clears up objects that no longer have any references to them. So if you create an object inside a function when the function exits the objects are marked for garbage collection as long as no variable outside the function has a reference to the object. If the objects are being passed into the function and when the function exits the objects are only marked for garbage collection if there is no variable that points to it. Also the garbage collector does not run all the time so when an object is disposed of it may be a while before it is actually removed from memory and it generally takes two passes of the garbage collector to remove an object from the managed memory.

If you post the code we can have a look at it.

Fernando
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 17161907
Hi bramsquad;

Here are two links to MSDN Magazine artical part 1 and part 2 on how garbage collector works.

Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework

Part 1
http://msdn.microsoft.com/msdnmag/issues/1100/gci
Part 2
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2

Fernando
0
 
LVL 6

Accepted Solution

by:
NunoGodinho earned 300 total points
ID: 17164054
Hi bramsquad,

  The problem is even if the object go out of scope the Garbage Collector is not going to destroy them in the moment that the function scope ends, because it runs in a predefined and scheduled time as the msdn explains. The only thing you can do is to say to Garbage Collector to run by using the GC.Collect(), but first you should make your objects equal to null. This is not a best practice but in this cases can help.

Hope it helpes,

 Nuno Godinho
0
 
LVL 8

Author Comment

by:bramsquad
ID: 17173495
thanks guys, i put in GC.Collect() and it looks like it solves the problem.  however i cant set the values to null becuase of the compiler error

Cannot convert null to 'System.Drawing.Rectangle' because it is a value type

thanks for the help!

~b
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now