了解JVM内存分配和Java内存不足:堆空间 [英] Understanding JVM Memory Allocation and Java Out of Memory: Heap Space

查看:159
本文介绍了了解JVM内存分配和Java内存不足:堆空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何真正地了解JVM中的内存分配方式. 我正在编写一个内存不足的应用程序:堆空间异常.

I'm looking into really understanding how memory allocation works in the JVM. I'm writing an application in which I'm getting Out of Memory: Heap Space exceptions.

我知道我可以传入Xms和Xmx等VM参数来增加JVM为运行中的进程分配的堆空间.这是解决该问题的一种可能的方法,或者我可以检查我的代码是否存在内存泄漏并在那里解决问题.

I understand that I can pass in VM arguments such as Xms and Xmx to up the heap space that the JVM allocates for the running process. This is one possible solution to the problem, or I can inspect my code for memory leaks and fix the issue there.

我的问题是:

1)JVM如何实际为其分配内存?这与OS如何将可用内存传递给JVM有什么关系?或更笼统地说,任何进程的内存分配实际上如何工作?

1) How does the JVM actually allocate memory for itself? How does this relate to how the OS communicates available memory to the JVM? Or more generally, how does memory allocation for any process actually work?

2)虚拟内存如何发挥作用?假设您有一个具有32GB物理内存的系统,并且已将所有32GB分配给Java进程.假设您的进程实际上消耗了所有32GB的内存,那么我们如何强制该进程使用虚拟内存而不是遇到OOM异常?

2) How does virtual memory come into play? Let's say you have a system with 32GB of physical memory and you allocate all 32GB to your Java process. Let's say that your process actually consumes all 32GB of memory, how can we enforce the process to use virtual memory instead of running into OOM exceptions?

谢谢.

推荐答案

JVM如何真正为自己分配内存?

How does the JVM actually allocate memory for itself?

对于堆,它分配一个最大大小的连续大内存区域.最初,这是虚拟内存,但是随着时间的流逝,它变成了在操作系统的控制下所使用部分的真实内存.

For the heap it allocate one large continuous region of memory of the maximum size. Initially this is virtual memory however, over time it becomes real memory for the portions which are used, under control of the OS

这与OS如何将可用内存传递给JVM有什么关系?

How does this relate to how the OS communicates available memory to the JVM?

JVM对操作系统中的可用内存一无所知.

The JVM has no idea about free memory in the OS.

或更笼统地说,任何进程的内存分配实际上如何工作?

Or more generally, how does memory allocation for any process actually work?

通常,它使用malloc和free.

In general it uses malloc and free.

虚拟内存如何发挥作用?

How does virtual memory come into play?

最初分配了虚拟内存,这将变为使用的实际内存.这对任何过程都是正常的.

Initially virtual memory is allocated and this turns into real memory as used. This is normal for any process.

假设您有一个具有32GB物理内存的系统,并且已将所有32GB分配给Java进程.

Let's say you have a system with 32GB of physical memory and you allocate all 32GB to your Java process.

不能.操作系统需要一些内存,并且将有其他用途的内存.即使在JVM内,堆也仅是所使用内存的一部分.如果您有32 GB的内存,我建议最大为24 GB堆.

You can't. The OS need some memory and there will be memory for other purposes. Even within the JVM the heap is only a portion of the memory used. If you have 32 GB of memory I suggest as 24 GB heap max.

假设您的进程实际上消耗了所有32GB的内存,

Let's say that your process actually consumes all 32GB of memory,

假设您有48 GB,然后启动一个使用32 GB主内存的进程.

Say you have 48 GB and you start a process which uses 32 GB of main memory.

我们如何强制该过程使用虚拟内存而不是遇到OOM异常?

how can we enforce the process to use virtual memory instead of running into OOM exceptions?

应用程序从一开始就使用虚拟内存.您不能使堆太大,因为如果堆开始交换,您的计算机(不仅仅是您的应用程序)将变得不可用.

The application uses virtual memory right from the start. You cannot make the heap too large because if it starts swapping your machine (not just your application) will become unusable.

通过谨慎使用堆外内存,您可以使用比物理内存更多的内存.但是托管内存必须位于物理内存中,因此,如果您需要32 GB的堆,请购买64 GB的主内存.

You can use more memory than you have physical by using off heap memory, carefully. However managed memory must be in physical memory so if you need a 32 GB heap, buy 64 GB of main memory.

这篇关于了解JVM内存分配和Java内存不足:堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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