如何列出在 jvm 中内化的字符串?

发布于 2024-11-09 14:43:04 字数 367 浏览 3 评论 0原文

可能的重复:
Java - 判断字符串是否被实习?

我想要已被 jvm 内部化的字符串列表,要么因为它们是文字,要么因为方法 实习生 被称为在他们身上。我怎样才能生成它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星光不落少年眉 2024-11-16 14:43:04

您可以获得所有驻留字符串的总大小:

$ 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
...

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
...
燕归巢 2024-11-16 14:43:04

如何生成它?

你不能在正在运行的程序中。没有用于迭代实习字符串池的 API。


理论上您可以通过调试代理来完成此操作。它将涉及:

  1. 遍历可到达的对象以查找所有 String 实例。
  2. 对于每一个,测试是否 str == str.intern()

然而,这个过程会很昂贵,并且会因为大量不必要的字符串而污染字符串池(永久堆)。此外,这仅在调试代理停止所有应用程序线程时才有效,因此应用程序无法使用此方法来检查其自己的字符串池。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文