Home arrow static arrow Java Programming [Archive] - how does collection at background work in java?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how does collection at background work in java?
1 Duke Stars available
This topic has 25 replies on 2 pages.    « Previous | 1 | 2 |

Posts:6,750
Registered: 1/25/04
Re: how does collection at background work in java?  
Jun 24, 2004 2:20 PM (reply 15 of 25)



 
I do not know about string pool but "a" is
declared/used in the class. When there is no need in
that class, it is unloaded and constants like "a"
should be removed from memory as well. That is why
I've called them "static".

The string pool is indeed the part you're missing. Even if the class is unloaded (which typically it never will be), the JVM maintains references to all string literals that have been used and so they do not get garbage collected.
 

Posts:835
Registered: 2/12/01
Re: how does collection at background work in java?  
Jun 24, 2004 2:44 PM (reply 16 of 25)



 
Even if the class is unloaded (which typically it never will be), the JVM maintains references to all string literals that have been used
Even if a class defines megabytes of literals? May be it is just a JVM incompleteness? IMO, GC of class (static) objects on class unload would be a natural soultion.
 

Posts:31,095
Registered: 4/30/99
Re: how does collection at background work in java?  
Jun 24, 2004 3:00 PM (reply 17 of 25)



 
Even if the class is unloaded (which typically it
never will be), the JVM maintains references to all
string literals that have been used

Even if a class defines megabytes of literals? May be
it is just a JVM incompleteness? IMO, GC of class
(static) objects on class unload would be a natural
soultion.

I believe a class is not allowed to define more than 64K bytes of string literals.

And as nasch said, classes are almost never unloaded so garbage collection of string literals in classes is not a major issue. I have no idea whether garbage collection removes entries from the string pool when no classes refer to them, but perhaps somebody else does.
 

Posts:9,091
Registered: 8/15/01
Re: how does collection at background work in java?  
Jun 24, 2004 3:08 PM (reply 18 of 25)



 
The string pool is indeed the part you're missing.
Even if the class is unloaded (which typically it
never will be), the JVM maintains references to all
string literals that have been used and so they do
not get garbage collected.

Surely the string pool is "emptied" from time to time if a large section of strings are no long used.

Also, do strings loaded from a file ( DataInputStream.readString() ) get placed in the string pool?
 

Posts:1,183
Registered: 1/23/02
Re: how does collection at background work in java?  
Jun 24, 2004 3:08 PM (reply 19 of 25)



 
I have no idea whether garbage collection removes entries from the string pool when no classes refer to them, but perhaps somebody else does.

It does at least in 1.3.1 (I've just checked the source).
 

Posts:835
Registered: 2/12/01
Re: how does collection at background work in java?  
Jun 24, 2004 3:10 PM (reply 20 of 25)



 
I believe a class is not allowed to define more than 64K bytes of string literals.

A limitation derived from 16-bit dos? :o)
 

Posts:6,750
Registered: 1/25/04
Re: how does collection at background work in java?  
Jun 24, 2004 3:11 PM (reply 21 of 25)



 
Also, do strings loaded from a file (
DataInputStream.readString() ) get placed in the
string pool?

I'm pretty sure it's only string literals that are put in the pool.
 

Posts:9,091
Registered: 8/15/01
Re: how does collection at background work in java?  
Jun 24, 2004 3:21 PM (reply 22 of 25)



 
I'm pretty sure it's only string literals that are put
in the pool.

Ahh, good. Thanks.
 

Posts:4,680
Registered: 6/14/99
Re: how does collection at background work in java?  
Jun 25, 2004 7:29 AM (reply 23 of 25)



 
I'm pretty sure it's only string literals that are put in the pool.

Ahh, good. Thanks.

That's my understanding as well. I ran this for a test:
import java.io.*; public class TestPool {  static String a, b;   public static void main ( String[] argv )  throws Exception {    BufferedReader buf = new BufferedReader(                         new FileReader( argv[0] ) );    a = buf.readLine();    if ( a != null ) b = buf.readLine();    buf.close();    System.out.println( a == b );  }}

... Where the file I read in - ab.txt - is simply two lines:
aa

... Comes back false.

~Bill
 

Posts:37,103
Registered: 3/30/99
Re: how does collection at background work in java?  
Jun 25, 2004 7:42 AM (reply 24 of 25)



 
I'm pretty sure it's only string literals that are
put
in the pool.

Ahh, good. Thanks.

Doesn't calling intern() also do it? Or is that a separate pool?
 

Posts:1,183
Registered: 1/23/02
Re: how does collection at background wiki/./work in java?  
Jun 25, 2004 7:49 AM (reply 25 of 25)



 
Doesn't calling intern() also do it? Or is that a separate pool?

One and same. I think the concern was that every String goes into the pool and nothing in the pool gets garbage-collected. We have since established that (1) not every String goes into the pool and (2) Strings in the pool do get garbage-collected.

I think there's a Ph.D thesis to be gotten from this groundbreaking research :-)
 
This topic has 25 replies on 2 pages.    « Previous | 1 | 2 |