How do you list the string that been internalized in a jvm? [duplicate]
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?
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
...
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:
- Traversing the reachable objects to find ALL
String
instances. - 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.
精彩评论