3

Possible Duplicate:
Java - tell if a String is interned?

I would like to have a list of the string that have been internalized by a jvm, either because they are literal are because the method intern as been called on them. How can I generate it?

Community
  • 1
  • 1
Guillaume Coté
  • 2,761
  • 2
  • 21
  • 28

2 Answers2

1

You can get the total size of all interned strings as:

$ jmap -permstat 543
Attaching to process ID 543, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 19.1-b02-334
14584 intern Strings occupying 1603648 bytes.
finding class loader instances ..Warning: skipping invalid TLAB for thread t@44819
...
Binil Thomas
  • 13,699
  • 10
  • 57
  • 70
1

How can I generate it?

You can't within a running program. There is no API for iterating the intern'd string pool.


You could in theory do it via a debug agent. It would involve:

  1. Traversing the reachable objects to find ALL String instances.
  2. For each one, testing if str == str.intern().

However, this process is going to be expensive, and is going to pollute the string pool (the permgen heap) with lots of Strings that have been interned unnecessarily. Besides, this only works when all application threads have been stopped by the debug agent, so an application can't use this approach to examine its own string pool.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This is theoretically doable on a small application, but it will fail with an out of memory in perm gen space with a huge application. Even if I increase the perm gen space enough, the goal is to make comparison of the list at different point. It won't be possible to do a valid comparison since the lecture modify the result. – Guillaume Coté May 26 '11 at 14:44