aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays and Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays and Garbage Collection" Watch "Arrays and Garbage Collection" New topic
Author

Arrays and Garbage Collection

Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
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?




Docendo discimus (we learn by teaching)
SCJP 5 Zertifizierung, Vorbereitungsbuch; SCJP 6 Zertifizierung, Vorbereitungsbuch

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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?


All code in my posts, unless a source is explicitly mentioned, is my own.
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
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?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Ankit, many thanks

that was it, replacing my delay with



solves the problem
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Dec 19, 2007
Posts: 64
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

Just read this...
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
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.
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
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

"Here I am, World, do your worst!"
"It may sound like you’re inviting bad luck and catastrophe, but what this really means is that you are welcoming life’s challenges with open arms and are willing to bravely take them on."-Atul Pawar.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.
anu sav
Ranch Hand

Joined: Jan 30, 2009
Posts: 47
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

Joined: Jan 30, 2009
Posts: 47
sorry for above..it got posted in reply by mistake
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
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

Joined: Dec 16, 2008
Posts: 1032
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.
 
Consider Paul's rocket mass heater.
 
subject: Arrays and Garbage Collection