当我没有创建任何实例时,为什么分析器会显示大量 char[] 实例?
我正在运行递归操作的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看字符串源代码。 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.这是内存分析的精妙之处,称为“保留大小”和“浅层大小”:
字符串就是一个完美的例子。它包含一些原始字段以及
char[]
。char[]
占据了绝大多数的内存使用量。 String 的浅层大小非常小,但它的保留大小要大得多,因为其中包括char[]
。NetBeans 分析器可能会为您提供浅层大小,这不是一个非常有用的数字,但很容易计算。保留的大小会将
char[]
内存使用量合并到String
内存使用量中,但计算保留大小的计算成本很高,因此探查器无法计算出来,直到明确要求。This is subtlety of memory profiling known as "retained size" and "shallow size":
String is the perfect example. It contains a handful of primitive fields, plus the
char[]
. Thechar[]
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 thechar[]
.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 theString
memory usage, but calculating the retained size is computationally expensive, and so profilers won't work that out until explicitly asked to.Sun 的 Java 实现中的
String
类使用char[]
来存储字符数据。我相信可以通过使用调试器查看
String
的内容或使用反射查看String
的内部结构来验证这一点,而无需查看源代码> 对象。因此,除非减少正在创建的
String
实例的数量,否则很难减少正在创建的char[]
的数量。The
String
class in the Sun's Java implementation uses achar[]
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 theString
object.Therefore, it would be difficult to reduce the number of
char[]
which are being created, unless the number ofString
instances which are being created were reduced.字符串由 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?