• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple but advanced question

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have the following code:

when the execution reaches the //outside scope the str variable still isn't avaiable for gabage colector. The str is not in scope anymore, but it will not be gabage collected there..

If i have the following code:

will the variable str garbage collected when the execution arrives //outside method scope?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. still its refering to the object but the reference is out of scope. If you did a reference=null, there is a possibility for the gc to pick up.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"My String" is a string literal and will not be garbage collected.
In the code:


the String object assigned to the String reference variable str is eligible for garbage collection at the line commented "//outside method scope".
[ October 27, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And in a similarly modified version of your first example:


the String object referred to by the String reference variable str is to be considered eligible for garbage collection at the line marked with the comment "// outside scope".
 
rogel garcia
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answering my own question...

Suppose whe had the examples with another object and not String...Integers for example
when you reaches // out of scope in the fist example the object has a reference althougt the variable isn�nt in scope anymore... so it ins't avaiable for garbage collection
when you are in // out of method scope .. the objects declared inside the method2 are now elegible for garbage collection...

I chaged the examples to another object than String because, Strings works a little diferent. There are pool of string litterals, something other objects doesnt have...
To discuss only about garbage collection on Strings objects i start a new topic named Are Strings garbaged Colected?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rogel garcia:
if you have the following code:

when the execution reaches the //outside scope the str variable still isn't avaiable for gabage colector. The str is not in scope anymore, but it will not be gabage collected there..

If i have the following code:

will the variable str garbage collected when the execution arrives //outside method scope?



I think there might be some confusion here. In your first example, when you reach the line labeled "outside scope" the object referred to by the reference variable str is eligible for garbage collection (yeah, so it's a String and Strings are special, but let's ignore that). At that point, the reference variable str has gone out of scope - it no longer exists. Therefore, there are no references to the String object it referred to and that object is now eligible for garbage collection.

In your second example, you have the exact same situation. When the method myMethod2() completes, the local variable str goes out of scope. When that happens, you no longer have a reference to your String object and that object would be eligible for garbage collection.

So, does that mean that the object will be GC'd when you get to your labeled lines? Probably not, but they might be. They'd be eligible for garbage collection. When the GC thread runs is entirely up to the JVM.
 
rogel garcia
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Corey McGlone but there is no mistake...


void myMethod(){
{
Integer integer = new Integer();
}
// here integer is not garbage collected
}


When the excecution arrives the commented line the variable integer WILL NOT be garbage collected.. Its outside scope but will not be elegible. Only when the method execution finishes it will be avaiable for garbage collection

Compile and run this class

Try this, and after uncomment the //integer = null;
You will se that the integer variable will only be garbage collected when it is explicited set to null, loosing the scope will not cause it to be garbage collected
execute the code with the following args for virtual machine -Xmx32M -Xms32M it is to don�t hava memory reallocation

I chaged from Integer to Integer[] because the Integer[] will consume more memory and the results will be easier to notice
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rogel,

I consulted with my sources and I still claim, according to the JLS, that the object will be eligible for garbage collection when the variable that refers to it goes out of scope.

Barry pointed me to this old thread which I'd long since forgotten about. Notice, at the bottom of that thread, there's a quote from Joshua Bloch, in his book, Effective Java:


"It should be noted that on present day JVM (JDK1.3) implementations, it is not sufficient merely to exit the block in which a variable is defined; one must exit the containing method in order for a reference to vanish."


So, it is really the JVM that is causing the object to be ineligible for garbage collection as it is not clearing out the reference variable when it should - when it falls out of scope. You assigning null to that variable forces the issue.

Barry also went as far as writing a test application which he ran in his environment (with utilizes Java 1.5). Here is what he came up with:



Here is the output he received (note that he added a few extra lines of output to make things a little more readable):



You can see from his example that the garbage collection does indeed take place. However, on my machine, I get different results - I do not see the garbage collection taking place (using Java 1.4.2 and Java 1.3.1). That leads me to believe that it is the specific implementation of the JVM that is causing the discrepancy.

As far as the SCJP exam goes, you're perfectly fine knowing that, once a reference goes out of scope, the object it refers to is eligible for garbage collection (assuming there are no other references to it). You do not need to concern yourself with that fact that some JVM's seem to deviate from this.

Here's a blurb from the JVM Spec regarding the heap and memory management:


"The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements."


So, I think it's fairly safe to say that anyone implementing the JVM may have a slightly different approach to this. In terms of the SCJP exam, however, when a reference goes out of scope, the object it refers to is immediately eligible for garbage collection.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It should be noted that on present day JVM (JDK1.3) implementations, it is not sufficient merely to exit the block in which a variable is defined; one must exit the containing method in order for a reference to vanish.



The jvm is supposed to make a last ditch attempt to free memory before throwing an OutOfMemoryError exception. However, if you are using blocks containing the only references to large objects as a memory management technique, you're out of luck.

This is why, when the performance chips are down, assembler code is best.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:

This is why, when the performance chips are down, assembler code is best.



And best of luck taking that application from your Windows machine to the Linux machine down the hall.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:

This is why, when the performance chips are down, assembler code is best.


All code ultimately must be reduced or transliterated to CPU instruction code before it can run. Do you mean to say that writing directly in assembler is best for performance?
[ October 27, 2004: Message edited by: Michael Ernest ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike Gershman wrote:

However, if you are using blocks containing the only references to large objects as a memory management technique, you're out of luck.



Of course not, this is not a technique to be recommended. It's only to prove that if garbage collection takes place the object in question could (not must) be collected. So it must be eligible for garbage collection.

I also found that one can get different results when using an IDE (BlueJ for example).
[ October 28, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This example:


when run with the BlueJ IDE produces:


Here you can see that garbage collection of the anonymous class object instance is performed after the block is exited but before the enclosing method (main) is exited.

However running the program at the command line does not have the same effect.
 
reply
    Bookmark Topic Watch Topic
  • New Topic