Solved

info about deleting objects

Posted on 2006-07-22
4
302 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

10 Experts available now in Live!

Get 1:1 Help Now