Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
please check the code snippet and can you explain how the garbage collection is working here...

C#
A obj1 = new A();
obj1.x = 22;
A obj2 = new A();
obj2.x = 33;
Console.WriteLine(obj1.x);
Console.WriteLine(obj2.x);
obj1 = obj2;
Console.WriteLine(obj1.x);
Console.WriteLine(obj2.x);


The question is that...
when we run the program, obj1 and obj2 will create the separate memory allocation..

but when we will assign obj1=obj2

obj2 we can deallocate , because it is not useful further...right...?
Why i am saying, we can't refer this obj2 from this point in the program. so the garbage collector will deallocate the memory or not??
Posted

Not quite.
You construct two objects, and assign them to different variables - one gets an x property of 22 so we will call this "x22" the other gets 33, so we will call this x33

obj1 refers to x22
obj2 refers to x33


You then assign the content of obj2 to obj1

obj1 refers to x33
obj2 refers to x33


x22 is no longer referenced, so the garbage collector can Dispose of it. obj1 and obj2 both can be used, but they refer to the same object in memory - and change made via obj1 will be seen via obj2 as well.
 
Share this answer
 
Comments
samu4u 13-Apr-12 4:10am    
Thanks,
But the garbage collector can understand, it will be use or not??
Or a programmer has to collect it??
OriginalGriff 13-Apr-12 4:19am    
It depends. With most objects, you can happily leave it to the GC to deal with, and it will get round to it when it neds to.
If you object involves scarce resources though, you should call Dispose yourself to release them when you have finished with it. These include (but aren't limited to) Graphics contexts and related objects, file and stream objects, bitmaps, and so forth. Generally, if it implements IDisposable and you create it via "new", then you should Dispose it when you are finished to prevent memory leaks.
samu4u 13-Apr-12 4:24am    
Did you felt the question is poor..??
OriginalGriff 13-Apr-12 4:34am    
No, why do you ask?
samu4u 13-Apr-12 4:53am    
I am new to this.. thats why??
obj1 = obj2 assigns the object being referred by obj2 to the obj1 and the object being referred earlier by obj1 is now detached from obj1.
In the present code obj1 and obj2 both are in the scope, hence, the obj2 will not be deallocated and both obj1, obj2 refer to the same object.
Only the object earlier referred by obj1, is no longer referred by obj1, and will be garbage collected by GC at some point.
 
Share this answer
 
v2
Comments
VJ Reddy 13-Apr-12 4:15am    
Any reason for down voting.
samu4u 13-Apr-12 4:19am    
answer is fine... i am new to this fourm..
VJ Reddy 13-Apr-12 4:24am    
Down voting an answer indicates that the answer is not appropriate. So, to know the deficiency, always give your reason for down voting, so that the author of the solution will come to know the deficiency and may improve the solution, which will be beneficial for the person asking the question and for the person giving the answer and others who may see the post. Further, generally it is a practice in this forum to vote 5 if there is no deficiency in the answer.
Thank you for your response.
samu4u 13-Apr-12 4:21am    
How we can able to use GC and if we are defining Collect() method then what are the things we have to make sure..

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