This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Regarding gc  RSS feed

 
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;
 
author and iconoclast
Posts: 24203
40
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.
 
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
author
Posts: 4096
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: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!