Click here to Skip to main content
15,884,835 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
What is garbage collection in .net ?
What is the use of that?
Should we use it in all our code\projects?
How to do it in code?
Please get me some code real life example ?
Posted
Updated 17-Feb-15 21:07pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-15 3:20am    
Did you try to read something on the topic? Asking questions when you don't yet have a clue (apparently) is possible, but is not the most efficient way of getting qualified help. It's much better to prepare your questions thoroughly. Not only it will give you more, it can also save a lot of your and our time. Invest good deal of time to safe even more future time.
—SA
Am Gayathri 18-Feb-15 3:58am    
Ok..I agree with you.

Use garbage collection by knowing that it exists and by never touching it. Why? To start with, understand how it works: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)[^].

[EDIT]

Also, this understanding should bring you to the understanding that memory leaks in managed code are still possible, but hardly random ones, mostly by design. To avoid them, all you need is, again understanding — understanding of how not to do something really stupid. Please see my detailed answers on related issues:
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
Memory leak in WPF DataBinding[^],
deferring varirable inside the loop can cuase memory leak?[^],
Garbage collectotion takes care of all the memory management[^].

—SA
 
Share this answer
 
v2
Comments
CPallini 18-Feb-15 3:18am    
Exactly: garbage collector is useful if you rarely have to deal with it.
5.
Sergey Alexandrovich Kryukov 18-Feb-15 3:21am    
Thank you, Carlo.
I actually added a lot more to this answer after you voted... :-)
—SA
Am Gayathri 18-Feb-15 3:29am    
Thanks SA.
Sergey Alexandrovich Kryukov 18-Feb-15 3:33am    
Sure. Will you accept the answer formally now?

(By the way, it's not correct personal addressing, by second name; it can be Sergey, Sergey Alexandrovich, or, in my case, also SA. No problem though, just for your information.)

—SA
TheRealSteveJudge 18-Feb-15 3:43am    
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
5*
To more about the Garbage Collector, go through Garbage Collection in .NET[^] and many other pages available on the internet.

You cannot do much with GC through managed code.
Check the dispose method for marking objects as ready to delete - Dispose method[^].
 
Share this answer
 
v2
Comments
Am Gayathri 18-Feb-15 3:28am    
Thanks Abhinav.. :)

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