Java堆空间--Xmx如何工作? [英] Java Heap Space - How does -Xmx work exactly?

查看:94
本文介绍了Java堆空间--Xmx如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中遇到了臭名昭著的 OutOfMemoryException ,而不是简单地增加可用的堆空间量,而是尝试研究问题出在哪儿,以防万一发生某种泄漏从我的应用程序中.

I have encountered the infamous OutOfMemoryException in my application and instead of simply increasing the amount of Heap Space available I tried to look into what the problem was, just in case, there was some sort of leak from my application.

我添加了JVM参数 -XX:+ HeapDumpOnOutOfMemoryError ,该参数在遇到OutOfMemory错误时会创建一个堆转储.然后,我分析了使用不同的分析工具生成的转储文件.然后,我开始使用-Xmx参数并观察模式.

I added the JVM parameter -XX:+HeapDumpOnOutOfMemoryError which creates a Heap Dump when the OutOfMemory Error is encountered. I then analysed the dump file produced with different profiling tools. I then started playing around with the -Xmx parameter and observing patterns.

让我感到困惑的是以下内容.为什么在分析转储时发现所有对象的总大小比我使用-Xmx参数设置的总大小小? 例如,假设我将-Xmx设置为'2048m'.分析转储文件时,我在堆上发现了总共400Mb的对象.我原本希望找到2GB.我想念什么吗?

What puzzled me is the following. Why is it that on analyzing the dump I found that the total size of all objects was much less than the total that I set using the -Xmx parameter? For example, say I set the -Xmx to '2048m'. When I analyzed the dump file I found a total of 400Mb of objects on the Heap. I was expecting to find 2GB. Am I missing something?

推荐答案

重新阅读您的错误消息-它可能会告诉您耗尽了哪种类型的内存.我猜这是PermGen空间. Permgen用于类定义(以及其他).您可以通过 -XX:MaxPermSize 调整PermGen的空间,PermGen不属于堆,因此不包含在堆转储中.

Re-read your error message - it may tell you which type of memory you ran out of. I'd guess that it was PermGen space. Permgen is used for class definitions (amongst other things). You can tune the space for PermGen via -XX:MaxPermSize PermGen isn't part of the heap, so isn't included in heap dump.

有关查看PermGen的更多信息,请参见此答案.

See this answer for more information on looking at PermGen.

如果这不是问题,请尝试将初始堆大小(-Xms)设置为与最大堆大小相同.这样做意味着堆不会增长,这将使了解正在发生的事情变得更加容易.

If this isn't the issue, then try setting your initial heap size (-Xms) to the same as the maximum. Doing this means that the heap won't grow which should make understanding what's going on easier.

我建议使用jvisualvm(JDK的一部分)在程序运行时查看您的内存利用率.

I recommend using jvisualvm (part of the JDK) to look at your memory utilisation as your program runs.

这篇关于Java堆空间--Xmx如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆