How to "free / dispose" objects?

  • Thread starter Thread starter Boris Nienke
  • Start date Start date
B

Boris Nienke

Hi,

i'm really new to .NET and i know, that the garbage-collection should free
objects itself... but is there any way to force this?

for example: i have a loop in which i need to create a new object every
time, fill in some data, store it to a database and then... free it...

[pseudo-code]
while (readline(myLine) != eof)
{
TMyObj tempObject = new TMyObj;
tempObject.abc = "abc";
//...more data to fill

StoreObject(tempObject)

//in pascal i would do:
// tempObject.Free;

}
[/pseudo-code]

The loop will run thru about 100000 times - so 100000 objects are created
but only used for a very short time!

I need a new object to have a new reference - so a Clear-Function to clear
my data is not enough.

Question: WHEN will the garbage-collection free the instances of the loops
before?

Boris
 
Generally you call GC.Collect to force collection, but it is not guaranteed
to happen. Still try it - that's pretty much your only recourse
 
A better recourse is to not create a new object each time in the loop, but
instead create one temp that is re-initialized each time through the loop.
The allocation every time will be a performance hit (and if memory is low
will trigger the GC to find the unused previously allocated objects and
clean them up causing still further performance hits. It's usually a pretty
bad idea to allocate from within a loop like that as even in PASCAL/C++ or
other languages with explicit allocation you have to do the allocate and
de-allocate every time through the loop, which is a waste of CPU time. The
other option is to make TMyObjtempObject a value type (if it is reasonable
small in size) and declare an instance within the {} for the while this will
simply create stack based temporary that is re-initialized on each loop
iteration.

e.g.:
[pseudo-code]
TMyObj tempObject = new TMyObj;

while (readline(myLine) != eof)
{
tempObject.abc = "abc";
//...more data to fill

StoreObject(tempObject)

}
[/pseudo-code]
 
Instead of creating a new object on each iteration of the loop, why
not consider creating the tempObject outside of the loop, and over
write the existing data on each iteration of the loop. That way you
only have 1 object that gets used over and over again, this way also
saves you overhead for creating new objects.

btw, setting the reference of an object to null helps the garbage
collector clean up data. EX:

tempObject = null;
 
Generally you call GC.Collect to force collection, but it is not guaranteed
to happen. Still try it - that's pretty much your only recourse

will try it - thank you! Never heard of "GC." before (as said: i'm pretty
new to the .Net-Stuff ;-) )

boris
 
A better recourse is to not create a new object each time in the loop, but
instead create one temp that is re-initialized each time through the loop.
The allocation every time will be a performance hit (and if memory is low
will trigger the GC to find the unused previously allocated objects and
clean them up causing still further performance hits. It's usually a pretty
bad idea to allocate from within a loop like that as even in PASCAL/C++ or
other languages with explicit allocation you have to do the allocate and
de-allocate every time through the loop, which is a waste of CPU time.

Yes - i know... normally i really wouldn't do that! But in this case i
really need a "new" object with a new pointer for the "StoreObject" to
recognize, that it's a different object (= do an insert) and not the same
(= do an update)
[pseudo-code]
TMyObj tempObject = new TMyObj;

while (readline(myLine) != eof)
{
tempObject.abc = "abc";
//...more data to fill

StoreObject(tempObject)

}
[/pseudo-code]

Yes - it's better style.. but it's always the same object so i propably
would have a problem to decide if i need an insert or update....
"StoreObject" is not my code so i cannot work with a bool-value like
"doUpdate = FALSE" or something like that.

And i would have the same question: how to free the "tempObject" when i'm
thru? ... i don't really like the idea to let "some system" free my Objects
"some times" ... ;-)

thank you anyway

boris
 
Instead of creating a new object on each iteration of the loop, why
not consider creating the tempObject outside of the loop, and over
write the existing data on each iteration of the loop.

see what i've written as reply to Steve...
btw, setting the reference of an object to null helps the garbage
collector clean up data. EX:

tempObject = null;

Ah, OK - i will do this ... makes sense

thank you

Boris
 
Boris,

Generally it's best to let the GC run when it wants to. I guess that's why
we don't talk about it much here and why you didn't here about it before.
;-)
 
Hi Boris,

I would recommend checking this online documentation. The first link is to
the Common Language Runtime section and the section is to the Automated
Memory Management section which describes the GC and has links to more
technical sections.

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconthecommonlanguageruntime.asp
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconautomaticmemorymanagement.asp

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Then you have no choice but to set the pointer to null before finishing each
iteration of the loop. It is a really poor implementation of the Store
function if it can't do a true member equality test instead of just a
pointer reference comparison as that puts the work of tracking and
preventing duplicates on the user of the class instead of on the
implementation of the class where it belongs. Are you *ABSOLUTELY* sure that
passing in the same object with different values will not work correctly
(That would be most unusual for a .NET implementation). Remember, you can't
really think of it as a pointer as the language and runtime don't treat it
that way semantically

Whether or not you like the idea of a garbage collector handling the
de-allocation isn't particularly relevant. That's a fundamental aspect of
how the .NET runtime operates and you have to live with it if you're going
to use C# or VB.NET. It really is more efficient. (Check out Walter Bright's
discussion of garbage collection for the 'D' programming language from
http://www.digitalmars.com/d/garbage.html )
 
implementation of the class where it belongs. Are you *ABSOLUTELY* sure that
passing in the same object with different values will not work correctly
(That would be most unusual for a .NET implementation). Remember, you can't

no - not 100% sure ... i will check it out soon because i really don't like
to create a few-thousend objects in a loop ;-)
Whether or not you like the idea of a garbage collector handling the
de-allocation isn't particularly relevant. That's a fundamental aspect of
how the .NET runtime operates and you have to live with it if you're going
to use C# or VB.NET.

i know :-S ... but i need to learn and understand a LOT about .NET and
these things before i really trust it ;-)
You know, i've learned, that this code would produce huge memory-leaks
(without freeing the objects) ... and now i learn, that this is OK because
there's a "House keeper" who is collecting all the garbage and throw it
away :-D ... strange....

Boris
 
Back
Top