当我没有创建任何实例时,为什么分析器会显示大量 char[] 实例?

发布于 2024-08-16 03:27:33 字数 212 浏览 3 评论 0原文

我正在运行递归操作的 NetBeans 配置文件,其中包括创建带有 java.lang.String 字段的类。在配置文件堆转储的类列表中,String 字段的数量对应于按预期创建的类的数量,但也有类似数量的 char[] 实例。字符数组占内存使用量的近 70%(!),而字符串字段约占 7%。

这是怎么回事?如何减少 char[] 实例的数量?

谢谢

I am running a NetBeans profile of a recursive operation that includes creating a class with a java.lang.String field. In the classes list, in the profile heap dump, the number of String fields corresponds to the number of classes created as expected, however there are also a similar number of char[] instances. The char arrays account for nearly 70% of the memory usage(!) whilst the String field accounts for about 7%.

What is going on here? And how can I reduce the number of char[] instances?

Thanks

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

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

发布评论

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

评论(4

心不设防 2024-08-23 03:27:33

查看字符串源代码。 String 对象本身包含缓存的哈希码、字符数计数(同样,出于优化目的)、偏移量(因为 String.substr() 指向原始字符串数据)< em>和字符数组,其中包含实际的字符串数据。因此,您的指标显示 String 消耗相对较少,但大部分内存都由底层字符数组占用。

Take a look at the String source code. The String object itself contains a cached hash code, a count of the number of characters (again, for optimisation purposes), an offset (since a String.substr() points to the original string data) and the character array, which contains the actual string data. Hence your metrics showing that String consumes relatively little, but the majority of memory is taken by the underlying character arrays.

谜泪 2024-08-23 03:27:33

char数组占比近70%
内存使用情况(!),同时
String字段占比7%左右

这是内存分析的精妙之处,称为“保留大小”和“浅层大小”:

  • 浅层大小指的是一个对象占用了多少内存,不包括任何子对象它包含的对象。基本上,这意味着原始字段。
  • 保留大小是浅层大小加上该对象引用的其他对象的大小,但仅由该对象引用的其他对象(很难解释) ,简单的概念)。

字符串就是一个完美的例子。它包含一些原始字段以及 char[]char[] 占据了绝大多数的内存使用量。 String 的浅层大小非常小,但它的保留大小要大得多,因为其中包括 char[]

NetBeans 分析器可能会为您提供浅层大小,这不是一个非常有用的数字,但很容易计算。保留的大小会将 char[] 内存使用量合并到 String 内存使用量中,但计算保留大小的计算成本很高,因此探查器无法计算出来,直到明确要求。

The char arrays account for nearly 70%
of the memory usage(!) whilst the
String field accounts for about 7%

This is subtlety of memory profiling known as "retained size" and "shallow size":

  • Shallow size refers to how much memory is taken up by an object, not including any child objects it contains. Basically, this means primitive fields.
  • Retained size is the shallow size plus the size of the other objects referred to by the object, but only those other objects which are referred to only by this object (tricky to explain, simple concept).

String is the perfect example. It contains a handful of primitive fields, plus the char[]. The char[] accounts for the vast majority of the memory usage. The shallow size of String is very small, but it's retained size is much larger, since that includes the char[].

The NetBeans profiler is probably giving you the shallow size, which isn't a very useful figure, but is easy to calculate. The retained size would incorporate the char[] memory usage into the String memory usage, but calculating the retained size is computationally expensive, and so profilers won't work that out until explicitly asked to.

梅倚清风 2024-08-23 03:27:33

Sun 的 Java 实现中的 String 类使用 char[] 来存储字符数据。

我相信可以通过使用调试器查看 String 的内容或使用反射查看 String 的内部结构来验证这一点,而无需查看源代码> 对象。

因此,除非减少正在创建的 String 实例的数量,否则很难减少正在创建的 char[] 的数量。

The String class in the Sun's Java implementation uses a char[] to store the character data.

I believe this can be verified without looking at the source code by using a debugger to look at the contents of a String, or by using reflection to look at the internals of the String object.

Therefore, it would be difficult to reduce the number of char[] which are being created, unless the number of String instances which are being created were reduced.

零時差 2024-08-23 03:27:33

字符串由 char 数组支持,因此我认为您不能在不减少字符串的情况下减少 char[] 实例的数量。

您是否尝试删除一些字符串以查看 char[] 是否也会下降?

Strings are backed by char arrays, so I don't think you can reduce the number of char[] instances without reducing your Strings.

Have you tried removing some Strings to see if the char[]s go down as well?

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