3

Consider the following object

class someObject;
{
   public objectA A{get;set;}
   public objectB B{get;set;}
   public objectC C{get;set;}
   public objectD D{get;set;}
   //...
}

at some point ObjectD is not needed anymore but the Instance of someObject still exist and will do so for couple hours, objectD takes up alot of memory so after its role is done i did set it up to null.

  • does that mean the memory will get freed from ObjectD as soon as i set it to null?
  • if an object sets for so long unused in some scope what does .NET do about it?
Liath
  • 9,913
  • 9
  • 51
  • 81
FPGA
  • 3,525
  • 10
  • 44
  • 73

6 Answers6

6

does that mean the memory will get freed from ObjectD as soon as I set it to null?

It's not object sets to null - its reference to object in memory sets to null. And object will exist until Garbage Collector will collect it (if you don't have any other references to this object).

Take a look on Fundamentals of Garbage Collection

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • so the Garbage Collector collects when there are no more references to it? is there a way to make the GC act instatly against some object? – FPGA Dec 19 '13 at 11:40
  • 3
    Note that the garbage collector's habits are actually different depending on the build configuration you've selected. Release builds are much more aggressive at pruning unused objects – Liath Dec 19 '13 at 11:40
  • 1
    @user1492051 you can run garbage collection manually, but that is pretty expensive operation and it will affect all garbage objects in application, not just this one – Sergey Berezovskiy Dec 19 '13 at 11:43
  • 3
    @user1492051 see my comment on one of the other questions - it is indeed possible to force a collection but this often does more harm than good. The .NET garbage collector is actually pretty good, you run a very high risk if you force it yourself because you push objects into the 2nd and 3rd generations which wouldn't normally be there. – Liath Dec 19 '13 at 11:44
  • 1
    @user1492051 agree with Liath - don't force garbage collection start. If your application don't need to allocate new memory on heap, then your dead object is not a problem. If free memory will be required, GC will collect all garbage automatically – Sergey Berezovskiy Dec 19 '13 at 11:48
1

The GC will collect items that are not referenced anymore. When it does that is up to itself, though.

So: Yes, setting the reference to ObjectD to null will enable the GC to collect it. No, it will (most likely) not happen immediately.

germi
  • 4,628
  • 1
  • 21
  • 38
1

You can set object as null followed by a Collect method Gc.Collect(). It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection.

S.N
  • 4,910
  • 5
  • 31
  • 51
  • 2
    I'd warn against forcing garbage collection yourself. Second and Third generation objects can take a lot more time and effort to reclaim, by forcing additional collections yourself (while it's possible you'll free some resources) it's much more likely that you'll force items into these later generations and actually damage your performance. – Liath Dec 19 '13 at 11:41
1

If you set D to null, it can be deleted from memory when GC will be called. But it is not a good solution to have null fields in your object. If it needed temporary, maybe it should be a local variable!?

alexmac
  • 19,087
  • 7
  • 58
  • 69
1
if an object sets for so long unused in some scope what does .NET do about it?

To reclaim memory the garbage collector collects and destroys objects which are no longer available which is when there are either no references to it, all the references are set to null, or else all references to it are from other objects that can be collected. The process of a collection involves moving available objects into the memory and reclaiming the memory used by objects which are no longer used. An object Which survives a collection is automatically promoted to the next generation

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
1
  1. The memory will NOT be freed immediately by setting it to NULL. The memory will be marked for garbage collection. The .Net GC scheme is somewhat efficient. If you are concerned, you can force a a GC cycle: See this stack overflow article on the yin/yang of this.

  2. If it sets unused, .Net will not do anything. That is to say, if you don't specifically "nullify" the reference, .Net has no reason to expect the object won't be needed (immediately) at some point. If you need it and .Net "temporarily removed it from memory", it will need to recreate it somehow, using information about the state of your class...the obvious choice for the restoration information is the class itself. So removing it is not really an option.

Offloading it through various stages of slower-but-larger memory access is usually the strategy used in modern computers: (1) Store memory in local on chip memory cache(s) for most recent data/program used. (2) Store memory in off chip memory chips. (3) Store memory in virtualized media (e.g. hard drive).

At the end of the day, you probably know best when to nullify the reference and the framework knows best about when/where to store the memory for your objects. There are exceptions, such as game programming, where the GC mechanism can cause hiccups. But in general, the system does well as long as you do.

Was this helpful?

Community
  • 1
  • 1
FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28