为什么我的 Java 堆有这么多字符 []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20388081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:35:04  来源:igfitidea点击:

Why my Java heap is with so many char[]

javaperformancetomcatmemory-leaks

提问by Nestor Hernandez Loli

I have a web application in Tomcat where I do many String operations (substring, indexof, trimming, etc.). I made a heap dump with jmap and I loaded it using VisualVM and I realized that near 50% of my heap memory usage is with char[], Why the char[] are taking that memory usage?, should I be concerned?, is it something related to String pools?enter image description here

我在 Tomcat 中有一个 Web 应用程序,我在其中执行许多字符串操作(子字符串、索引、修剪等)。我用 jmap 做了一个堆转储,我用 VisualVM 加载它,我意识到我的堆内存使用量的近 50% 是用 char[],为什么 char[] 占用内存使用量?,我应该担心吗?,是它与字符串池有关吗?在此处输入图片说明

回答by Thijser

Strings are internally just a char[] and some extra data. A char[] means a character array in other words it's an array that saves your string character by character. If you do a lot of string proccessing it's entirely possible that your system is fileld with char arrays.

字符串在内部只是一个 char[] 和一些额外的数据。char[] 表示字符数组,换句话说,它是一个逐字符保存字符串的数组。如果您进行大量字符串处理,则您的系统完全有可能使用字符数组进行归档。

So in short nothing to be concerned unless you system is actually using more memory then it should be. In that case you can look if there are some file structures left uncleared (hash maps or related).

所以简而言之,除非您的系统实际上使用了更多的内存,否则没有什么可担心的。在这种情况下,您可以查看是否有一些文件结构未清除(哈希映射或相关)。

回答by Andrey Chaschev

811k of char[]correspond to 800k of Strings, so yes, you have too many Strings.

811k ofchar[]对应 800k Strings,所以是的,你有太多的字符串。

If you're having a memory leak (judging by the tag), then it's most probably are strings in a HashMap. You have 800k instances of strings, 200k instances of hash entries, 30k of maps. This probably means that your strings are kept in this cache and are not removed. Global map is a frequent cause of memory leaks, one needs to make sure that they are evicted from this cache.

如果您遇到内存泄漏(根据标签判断),那么它很可能是HashMap. 您有 800k 个字符串实例、200k 个哈希条目实例、30k 个映射。这可能意味着您的字符串保留在此缓存中并且不会被删除。全局映射是内存泄漏的常见原因,需要确保将它们从该缓存中逐出。

Since all these values are not being GC-ed, you could try analyzing an object graph to see what's holding them with a tool like JProfiler.

由于所有这些值都没有经过 GC 处理,您可以尝试使用JProfiler 之类的工具分析对象图以查看保存它们的内容。