• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding gc  RSS feed

 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two arrays of two value objects

class VO1 {
String name;
String age;
//getter methods
//setter methods
}

class VO2 {
String name;
String age;
//getter methods
//setter methods
}

VO1[] objArry1 = VO1[10];
VO2[] objArry2 = VO2[20];

Assuming that both these arrays are populated, i am creating a third array of value objects by using these two arrays

ThirdVo[] objArry3 = ThirdVo[30];

For populating the thrird VO i am using for loop and getter methods of value object.
e.g
for (int i = 0; i<objArry1.length; i++) {
VO1 tempOBj = objArry1[i];

ThirdVo obj = new ThitdVo();
obj.setName(tempObj.getName());
obj.setAge(tempObj.getAge());

objArry3[i]= obj;

//for gc
objArry1[i] = null
}
//for gc
objArry1 = null; //as my objects are copied in ThirdVo and i don't want it further in the code

My Question
1.is the line after //for gc is really necessary
2.is it a good practice to make obj reference to explicitly to null

regards
prashant
objArry1[i] = nullobjArry1 = null;
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If objArray1 is a long-lasting array -- i.e., you'll keep a reference to it long after this code is done -- then setting its contents to null will free up those objects for GC sooner. If, on the other hand, the array pointed to by objArray1 is itself eligible for GC as soon as this code completes, then there's little point in setting its contents to null. Some people might argue that it makes "less work" for the GC, but in general, this doesn't matter.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For local variables it is not useful to set them to null at the end of the method because they go out of scope immediately after, also removing any reference they still had. It's even possible that compilers remove these statements for optimization.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Spoor:
For local variables it is not useful to set them to null at the end of the method because they go out of scope immediately after, also removing any reference they still had. It's even possible that compilers remove these statements for optimization.


This sounds a little misleading, a local variable may be made more permanent if a global pointer is set to it. In this case, the local variable would not go out of scope after the method terminated.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are talking about the object that variable refers to.
Like I said, the reference variable goes out of scope. I've said nothing about the object - the global rules of garbage collection hold for that.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott wrote:
This sounds a little misleading, a local variable may be made more permanent if a global pointer is set to it. In this case, the local variable would not go out of scope after the method terminated.


The variable will get a value null when it goes out of scope. The scope of the variable is in the block the variable is declared. The scope of variable starts from where the variable is declared and ends where the block ends. Since the scope of the variable is limited to the block/local such a variable is called local variable.


The method variable will go out of scope after the method terminates.


There is no global variable in java. To accomplish this you can declare a public class variable in a singleton or have a public static class variable.

The class variables and method variables are variable references. Garbage Collector is interested in freeing the resources which the Object was holding and this object is referenced by the variable.

The method variable will go out of scope when the method terminates. In sun's documentation, method variable is a variable declared inside a method.

If there is no reference to an object existing it could be garbage collected. By making the variable null (implicitly or explicitly) it is not guaranteed that all the references to that object is removed.
[ November 16, 2005: Message edited by: jiju ka ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!