字符串池大小是否受最大堆大小的设置影响?

huangapple go评论81阅读模式
英文:

Is the string pool size affected by setting max heap size?

问题

我有一个Java应用程序,其内存使用情况很奇怪,我注意到内存消耗显著高于最大堆大小(Xmx800m,使用量为1.4g)。

在此之前的最近更改之一是正在使用的唯一字符串大幅增加,所以我想也许我有很多字符串在堆外使用了大量内存 - 这种情况可能吗?

我正在运行Java 11。

编辑:

例如,在这篇文章中提到:

> 当我们使用new()运算符创建String对象时,它总是在堆内存中创建一个新对象。
另一方面,如果我们使用String字面语法(例如“Baeldung”)创建对象,如果该对象已经存在,它可能会从字符串池返回现有对象。

给人一种堆和字符串池两个不同区域的印象。

英文:

I have a java application that has a weird memory usage, I noticed that the memory consumption is significantly higher than the max heap size (Xmx is 800m, usage is 1.4g).

One of the recent changes preceding this was a large increase in the unique strings in use, so I thought maybe the many strings I have use a lot of memory outside the heap - is this possible?

I'm running java 11.

EDIT:

for example, in this article it mentions:

> When we create a String object using the new() operator, it always creates a new object in heap memory.
On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.

Giving the impression of 2 different areas - the heap and the string pool.

答案1

得分: 4

在JDK中,包含了存储了字符串常量池的两个部分:

  1. 哈希表,该表位于非堆上并包含指向字符串常量的引用。在本机内存跟踪报告中,这个内存区域被标识为“String table”。

  2. 字符串常量的内容。这些是位于Java堆上的普通java.lang.String对象。

因此,字符串常量池具有堆内和堆外的部分。堆外部分通常较小,但如果应用程序创建了过多的字符串常量,它仍然可能占用大量的额外内存。

使用本机内存跟踪(Native Memory Tracking)来查找字符串表消耗的内存量。

参见此答案以查找进程可能比-Xmx设置的内存限制消耗更多内存的其他原因。

英文:

In JDK, the string pool that contains interned strings, consists of two parts:

  1. The hash table, which is allocated off-heap and contains the references to the interned strings. This memory area is shown in Native Memory Tracking report as "String table".
  2. The interned strings contents. These are regular java.lang.String objects in Java heap.

So, the string pool has both on-heap and off-heap parts. The off-heap part is typically smaller, but it still can take a significant amount of extra memory, if the applications creates too many interned strings.

Use Native Memory Tracking to find the amount of memory consumed by the String table.

See this answer to find other reasons why the process may take much more memory than -Xmx.

答案2

得分: 2

自从Java 7以来,字符串池已经成为常规堆的一部分(参见这里)。因此,常规堆的大小也限制了字符串池的大小。

你应该从那篇文章中了解到的真正内容是,new String(...)始终会创建一个新的String对象,该对象与先前创建的所有String对象都不同。而对于与字符串字面值对应的String对象则不是这种情况。

> 我注意到内存消耗显著高于最大堆大小(Xmx为800m,使用量为1.4g)。

对此的解释是,JVM使用的内存有许多方式,并不是常规堆的一部分。这些包括:

  • JVM可执行文件及其共享库的内存
  • 应用程序加载的本地代码库使用的内存
  • 用于Java线程堆栈的内存段
  • 用于表示元空间的内存……其中保存了JIT编译的本机代码等等,
  • 由本机代码分配的“本机”堆的内存,例如使用malloc
  • 为直接缓冲区分配的内存
英文:

Since Java 7, the string pool has been a part of the regular heap (see here). So the regular heap size also constrains the size of the string pool.

The real thing you should be taking from that article is that new String(...) always creates a new String object that is distinct from all previously created String objects. That is not the case for a String object that corresponds to a string literal.

> I noticed that the memory consumption is significantly higher than the max heap size (Xmx is 800m, usage is 1.4g).

The explanation for that is that there are various ways that a JVM uses memory that is not part of the regular heap. These include:

  • the memory for the JVM executable and its shared libraries
  • memory used for native code libraries loaded by the application
  • memory segments used for Java thread stacks
  • memory used to represent the metaspace ... which holds JIT compiled native code and so on,
  • memory in the "native" heap allocated by native code; e.g. using malloc.
  • memory allocated for direct buffers

答案3

得分: -1

你可以使用 https://visualvm.github.io/index.html 来分析和检查,例如你的内存占用和堆情况。

英文:

You can use https://visualvm.github.io/index.html to analyse and inspect for example your occupied memory and heap.

huangapple
  • 本文由 发表于 2020年4月5日 20:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定