开发者

How do you list the string that been internalized in a jvm? [duplicate]

This question already has answers here: Closed 11 years ago. 开发者_如何学C

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:

  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜