• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regarding gc

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4354
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic