Question:
Garbage Collection in Java?
God Z
2012-02-25 19:02:43 UTC
I need help understanding garbage collection in java. Can you also please give an example. Also does this count as garbage collection:

public class Vir
{
public static void main(String[] args)
{
String a = "hello";
System.out.println("a = " + a);
String b = a;
a = "";
System.out.println("b = " + b);
System.out.println("A now = " + a);

}
}
Three answers:
anonymous
2012-02-25 19:14:52 UTC
EDIT: yes, after the second b assignment statement "b=a;", no variable point to the location where "hello" is stored, so that data is garbage and Java will automatically free up that memory for you

---------------------------------------------------------------------------------------



^err, no, b points to the same location a used to, so no garbage.



In java, objects do not hold the value, but rather a reference to the value.

so, after String a = "hello";, a does not actually hold "hello", but a reference to the location in memory where "hello" is stored.



Now, when you said a = "";, you are changing the reference stored in a to another location in memory where an empty string is stored. that is, a no longer points to the location where "hello" is stored. Now, here's why the example above doesn't count as garbage collection scenario. Since you said String b = a; before you changed a, b points to the same location as a used to. That means the string "hello" is still accessible by b.



Consider what would happen if String b = a; was never stated. Since a now references "", what happens to the memory occupied by the string "hello"? Since no object refer to that location anymore, it can no longer be accessed. It is considered garbage data. In a managed language like Java, it automatically cleans up this memory for you so you don't have to do anything to free up that memory that stores "hello". (In an unmanaged language like C++, the programmer has to delete the actual data from memory)
llaffer
2012-02-25 19:10:42 UTC
You'll never see garbage collection happening in a program. It's all automatic in the background.



But what you are doing WILL activate the garbage collector at the point you make a change to String a.



First, you set up a String a containing "hello". "hello" exists in memory as a string, and 'a' will point to that memory address.



Later on when you set a to a null string, you are not changing the memory address containing "hello" like you would be doing in C, but instead you are creating a new string, in new memory containing 0 bytes. Then a is associated with this new memory, leaving the old "hello" in memory no longer associated with any object.



This is when the garbage collector will do the work and clean up and make that memory available to the rest of the system again.



When main terminates, java will then deallocate the variables a and b, which will let the garbage collector clean that up, then java will end having ended all jobs.
pardeep
2014-03-15 02:41:21 UTC
http://java-latte.blogspot.in/2013/08/garbage-collection-in-java.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...