Hi.....
String a[] = new String(); String userName = ""; a[0]="Kalees"; userName = a[0]; a[0]=null;
My doubt is how many reference is created? and is ths array eligible for garbage collection?
Pls explain ths.....!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi.....
String a[] = new String(); String userName = ""; a[0]="Kalees"; userName = a[0]; a[0]=null;
My doubt is how many reference is created? and is ths array eligible for garbage collection?
Pls explain ths.....!
Last edited by kalees; September 20th, 2009 at 09:21 AM.
First a quick note: References and the actual object are different. Take for example a crime scene. There were 5 witnesses. They all say that John did it. John here is the actual object, but there were 5 references to him. The JVM's garbage collector "reclaims/deletes" objects when there are no more references to that object. So, in the previous example John did it, but since there were 0 witnesses, the cops would not have him as a suspect (given no other evidence that would say he did it).
Some syntax errors in your program:
String a[];
Remember, a[] is not a valid variable name. To declare an array of strings, you must do this:
String[] a;
Also, the second part of that line is incorrect. You can't create a String and say that is an array of Strings (well, in plain English, maybe but not in Java). Instead, you must do this:
Where 5 is the number of elements in the array a. Each element inside a is a reference to a string, so here there are 6 references (5 inside of a, and a itself).String[] a = new String[5]
As a general rule, references don't get garbage collected, objects do. To answer your question, if the first line had been modified as I stated above, you would have 7 total references (6 from a, 1 from userName). You would have the String object "Kalees" and the String[5] object at the end of this code because the "" String was garbage collected when it was no longer referenced by userName, and no other references were pointing to it.
At this point, the String[5] array is NOT eligible to be garbage collected because it is still referenced by a, even if all it contained were null's.
What he said, sort of. You can actually do this if you like.
String[] a[];
It does not matter where the brackets go however what helloworld said is the best practice. And also in your code example above if the syntax errors where fixed and that was the whole program nothing would get garbage collected. Not even if you did this at the last line.
a = null;
// Json
Oh, hrm. I didn't know about that first one.
As for the second one, are you sure? I know i've had this problem when working with singly linked lists that elements "disappear' when I remove references in the wrong order. Things get garbage collected when nothing points to them, not when they point to nothing. And I thought the "" string object got garbage collected, but I have no idea how the JVM garbage collects strings (it's some weird bizarre things, though).
Last edited by helloworld922; September 21st, 2009 at 10:23 AM.
Well if you do variable = null on the last line in your program before the program exits the garbage collector wont be run because there is no need. It will become eligible but it wont be GCed.
How about this for array declarations.
String[] a, b[], c[][], d[][][];
Ouch!
// Json
Then you're whole program becomes garbage collected.
hehe
// Json