Solved

info about deleting objects

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

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 63

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
List<PaisEntity> - show some 1 28
Can I command line build a C# application? 6 40
jquery + C#.net radiobuttonlist 3 28
Please explain purpose of GZIP 4 34
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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