Solved

info about deleting objects

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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