Reputation: 3865
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.
ObjectD
as soon as
i set it to null?Upvotes: 3
Views: 649
Reputation: 3397
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.
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?
Upvotes: 1
Reputation: 13784
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
Upvotes: 1
Reputation: 19617
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!?
Upvotes: 1
Reputation: 5140
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.
Upvotes: 1
Reputation: 4658
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.
Upvotes: 1
Reputation: 236308
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
Upvotes: 6