2

I came to the following phenomenon when tracing a possible memory leak. Is Java 7 reclaiming interned Strings or jmap not precise?

# jmap -heap 9724 | grep interned
Attaching to process ID 9724, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.3-b01
10526 interned Strings occupying 880048 bytes.

# jmap -heap 9724 | grep interned
Attaching to process ID 9724, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.3-b01
10514 interned Strings occupying 878984 bytes.

# jmap -heap 9724 | grep interned
Attaching to process ID 9724, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.3-b01
10519 interned Strings occupying 879720 bytes. 

Environment: Linux version 2.6.32-220.23.1.el6.centos.plus.x86_64 (mockbuild@c6b5.bsys.dev.centos.org) (gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP Tue Jun 19 04:14:37 BST 2012

java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b10) Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

1 Answers1

3

Yes Interned Strings are reclaimed. And as specified in Java SE 7 Features and Enhancements it is proposed that :

In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • I tested [the code](http://stackoverflow.com/questions/2431540/garbage-collection-behaviour-for-string-intern/2433076#2433076) in the post mentioned in Marko's comment using Java 6 in Eclipse 20121004-1855; well, the same behavior. JRE installed on my machine is 7, though. java version "1.7.0_09" Java(TM) SE Runtime Environment (build 1.7.0_09-b05) Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode) – user2081147 Mar 26 '13 at 19:53
  • @user2081147: Yeah I too had gone through that code while looking for the same gc stuffs against interned strings. It exhibits that interned Strings are GCed indeed because we are getting different hashcodes for same string interned. Moreover one thing more to see in this link http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html#par_gc.generations that Permgen contains virtual memory also..So when Permgen increases its max limit it is gced. That scenario was upto java6 . Now interned strings are not stored in permgen and still they are Garbage Collected as proved by this code. – Vishal K Mar 26 '13 at 20:02
  • Wow, @Vishal, great catch! I had never noticed the small grey purple area to the right. – user2081147 Mar 26 '13 at 21:33
  • Too small to be noticed ;) – Vishal K Mar 26 '13 at 21:37