Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
3.55/5 (4 votes)
See more:
How we can release un-use resources using garbage collector from our code?
Posted

In general you don't. You can cause the GC to activate by calling GC.Collect, but it's probably not what you actually need to do.

Instead, you should implement the IDisposable interface on all your classes which contain resources, and always either call Dispose or use a using block on all classes which implement it. Waiting for the GC (or even manually forcing it to activate) is a very poor practice, as well as being a very inefficient way to do things.
 
Share this answer
 
Comments
Tanumay99 26-Nov-13 4:32am    
Thanks
OriginalGriff 26-Nov-13 4:41am    
You're welcome!
 
Share this answer
 
Comments
Tanumay99 26-Nov-13 4:32am    
thanks
ArunRajendra 26-Nov-13 4:50am    
You welcome
In .NET framework, if the resource is in managed heap, GC takes care of it automatically.
Or implementing Dispose() can serve your purpose.
 
Share this answer
 
Comments
Tanumay99 26-Nov-13 4:32am    
Thanks
I would suggest you to instantiate your objects with using block.

It automatically disposes the object after use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900