• 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

Arrays and Garbage Collection

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given



Output: Killed 1 Object at line 18

Why is there only one object collected at line 18???

a should be garbage-collected in line 24 too! Is the collector not in the mood to do so???

In Java 6 System.gc() does not work any more as before.

Has somebody explanations?



 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Terence,

In general, gc() is just a request to perform garbage collection, but you can't rely on it being carried out. In this case, however, the behavior you are seeing is not due to the GC slacking off. Here's what's going on:

The only class that overrides the finalize() method in your code is the GcArray class, so the output you see takes place when you set the reference a to null. The GC seems to be running immediately this time, without giving you a chance to assign 24 to the static int variable.

Does that make sense?
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben

I oversaw that you see only the classes who override the finalize() method!

But it is too early at this line. The Message should come later, at line 24 instead at line 18, as a is still referenced at line 18.

To see the whole behavior I should override the finalize() method in the String class. But hell, you cannot override nor String or StringBuffer: both classes are final.

I help myself with an own class. Now with a class of my own MyString it seems to work:



Output

//I Killed 1 String
//I Killed 1 String
//I Killed 1 String
//Killed 1 Object at line 18

But the silly line 18 is still there, it should read 24!!!

Has the GC in Java 1.6 gone premature???or is it a programming error of mine?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terence I think the garbage collector is working more efficiently than you are expecting. In your code



when you set a to null, the garbage collector collects a even before you set line to 24 in the next statement. Now there are a few things here. You can depict delays in a better way like this



The other thing is that System.gc() never guarantees that garbage collection will be performed. It is just your request for garbage collection to run...
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, many thanks

that was it, replacing my delay with



solves the problem
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some variations of this problem:

If you replaced the existing array with an array of String literals, as



what happens?

a) Is there no a object, as String literals might be static and no objects?
b) Is there one object, as identical String literals go into the String pool?
c) Are there 3 objects?

I think c) can be excluded, as identical string literals go into the String pool.

But the decision if a) or b) is true is harder. The question is: Is the String pool an Object??? I think not.

So a) should be true???

Unfortunately there is no way to test this behavior with an overridden finalize method, as String is final.

What does somebody else think about?

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terence only identical strings don't go into the string pool. All string literals go into the String pool. So in your program if you write

String s1 = "Hello";
String s2 = "World";

Than both will go into the string pool. There was a very long discussion on this which you can see here...
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, I did not get the point!

My question: Is the String pool static or not? Is there any object involved in the String pool?

As I read in many examples:

String s1="hello";
and
String s2="hello";

are said to go to one single object in the String pool. It would implicitly mean, that there is an object involved. Is that wrong?

For me the string pool looks static, as there ist no new involved in the creation of a literal.

I had a look at the discussion you recommended, I cannot see a clear answer as what is what.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just read this...
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, very good reference.

String literals are objects and if they have the same content they are only one single object in the literal pool.

In the above example it means that the array consists only out of one object.

If we could overwrite finalize with Strings we would have only two objects, the one of the program and the one of the Strings in the array.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi terre..
infact GC in java 6 is more effective than ever.
in your code it comes in immediate action when the s is assigned as null.
and finalize method does the final display of GC stage at line 18
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atul Pawar wrote:hi terre..
infact GC in java 6 is more effective than ever.
in your code it comes in immediate action when the s is assigned as null.
and finalize method does the final display of GC stage at line 18


Atul, what do you mean that GC in java 6 is more effective than ever? I thought that GC behavior was left to the implementation and is not specified beyond some general guidelines.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii, given the following code:wht will be the output?
answer given is 1. But i think '2'. can anyone explain?

public class example {

int i[] = {0};

public static void main(String args[])
{
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
int j[] = {2};
i = j;
}
}
 
anu sav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for above..it got posted in reply by mistake
 
Atul Pawar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:

Atul Pawar wrote:hi terre..
infact GC in java 6 is more effective than ever.
in your code it comes in immediate action when the s is assigned as null.
and finalize method does the final display of GC stage at line 18


Atul, what do you mean that GC in java 6 is more effective than ever? I thought that GC behavior was left to the implementation and is not specified beyond some general guidelines.




Yes i do agree with you, actually i think i quoted in different way that GC in java i meant coomparing GC of java to other oop languages .
GC behaviour of course is left by Sun for implementation.

once again sorry for my wrong initial quote.
Thanks Ruben.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atul Pawar wrote:

Ruben Soto wrote:

Atul Pawar wrote:hi terre..
infact GC in java 6 is more effective than ever.
in your code it comes in immediate action when the s is assigned as null.
and finalize method does the final display of GC stage at line 18


Atul, what do you mean that GC in java 6 is more effective than ever? I thought that GC behavior was left to the implementation and is not specified beyond some general guidelines.




Yes i do agree with you, actually i think i quoted in different way that GC in java i meant coomparing GC of java to other oop languages .
GC behaviour of course is left by Sun for implementation.

once again sorry for my wrong initial quote.
Thanks Ruben.


Thanks for clarifying, Atul. I was just wondering if there was something in Java 6 regarding GC that I was not aware of.
reply
    Bookmark Topic Watch Topic
  • New Topic