Re: Setting objects to Nothing uses less memory!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

vdex42_at_hotmail.com
Date: 08/19/04


Date: 19 Aug 2004 13:51:53 -0700

Hi Chris,

Thanks for replying.

Do I understand correctly:

When the system reaches the end of the function that doesn't do the set
object to nothing: The garbage collector only does one pass and frees
"bigObjects", but does not recursively free anything it owns? (i.e.
that only happens in the second pass)

But when the system reaches then end of the function that sets
everything to nothing at the end: The garbage collector frees
"bigObjects" and its internal instance of "lotsOfData" in the first
pass , because I have explicitly forced the internal references to be
lost with my "InternalSetNull" function?

Thanks,
Ivan

"Chris Lyon [MSFT]" wrote:
> Hi Ivan
>
> I think people often get confused about setting references to null.
There is no hard rule that setting things to null will always or never
release memory sooner. In your code
> snippets, you're demonstrating a good example of when to set things
to null/Nothing, but maybe not how you expected.
>
> The key is your InternalSetNull() method. In the first example,
you're keeping your ReallyBigObjects references, which in turn keep
references to lotsOfData. By setting
> lotsOfData to null, you're telling the GC that you no longer need it,
but you still want to keep around your ReallyBigObjects. The GC will
then collect the lotsOfData arrays when it
> performs its next collection, but not necessarily your
ReallyBigObjects (since lotsOfData is really where the memory pressure
is coming from).
>
> So to sum up, setting instance variables to null has the effect
you're expecting (free memory sooner), while setting local variables to
null has little effect.
>
> Hope that makes sense!
> -Chris
>



Relevant Pages

  • Re: Setting objects to Nothing uses less memory!
    ... When the system reaches the end of a scope, the GC doesn't necessarily perform a collection. ... and only collects the objects with no live references, and promotes the rest to generation 1 or 2. ... In the function that didn't null anything, when a collection occured, everything got promoted a generation, since the bigObjects were live references to your lotsOfData. ... So setting member variables to null/Nothing while their enclosing objects are still alive, may be better on memory. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Python language problem
    ... references and you cannot free an object. ... The garbage collector will do it for you automatically, ... access the resource, ... I create same object in Tree, I want to update Tree, when I need to ...
    (comp.lang.python)
  • Re: FastMM and garbage collection...
    ... It seems to me that the memory manager has all the information required ... Such references could be pretty much ... So how would your garbage collector determine which objects are no ... total control of all threads and have complete type information about ...
    (borland.public.delphi.language.basm)
  • Re: Dispose must be thread-safe ?
    ... > I admit that due to my limited knowledge of GC-ing in .NET I was not able to ... > other thread still references it and even calls Dispose method on it)?? ... method would include an implicit "root" (from the garbage collector's ... it doesn't stop the garbage collector from trying to ...
    (microsoft.public.dotnet.framework.clr)
  • Re: does python have useless destructors?
    ... "Called by the garbage collector on an object when garbage collection ... determines that there are no more references to the object." ... Let Java code ... > use Java idioms. ...
    (comp.lang.python)