Solved

info about deleting objects

Posted on 2006-07-22
4
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses

628 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