JVM内存段分配 [英] JVM Memory segments allocation

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

问题描述

好吧,我有一个关于JVM的内存段的问题,
我知道每个JVM都会选择实现这个有点不同,但它是一个整体概念,应该在所有JVM的

运行时不使用虚拟机执行的标准C / C ++程序在运行时有四个内存段,
Code / Stack / Heap / Data $

然而,当一个JVM执行一个Java编译程序时,在运行时它有5个内存段

方法区/堆/ Java堆栈/ PC寄存器/本地堆栈

我的问题是这样的,谁分配和管理这些内存段?
操作系统不知道正在运行的java程序,并认为它是作为计算机上的常规程序运行的JVM的一部分,JIT编译,Java堆栈使用,这些操作需要运行时内存分配以及我无法理解的是JVM如何将内存分成这些内存段。
这绝对不是由操作系统完成的,并且这些内存段(例如Java堆栈)必须是连续的才能工作,所以如果JVM程序只是简单地使用诸如malloc的命令来接收堆内存的最大大小和内存分割,我们没有承诺连续内存,我会喜欢它,如果有人可以帮助我在我的脑海中得到这个直线,它们都混在一起......

$ b $ J

解决方案

当JVM启动时,它有数百个(如果不是数千个)内存区域。例如,每个线程都有一个堆栈以及一个线程状态区域。每个共享库和jar都有一个内存映射。注意:Java 64位不会像16位应用程序那样使用段。


谁分配和管理这些内存段?

所有内存映射/区域都是由操作系统分配的。


操作系统不知道正在运行的java程序,并认为它是作为计算机上常规程序运行的JVM的一部分,

JVM作为常规程序运行,但内存分配使用与正常程序相同的机制。唯一的区别是Java对象分配由JVM管理,但这是唯一以这种方式工作的区域。


JIT编译,Java堆栈使用,

JIT编译发生在一个正常的OS线程中,每个Java堆栈都是一个普通的线程堆栈。


这些操作需要运行时内存分配,

它大部分都使用 malloc 空闲 map unmap


我不明白的是一个JVM将它的内存分成这些内存段

它没有。堆仅用于Java对象。例如,最大的堆不是最大的内存使用量,只有一次可以拥有的最大数量的对象。


绝对不是由操作系统完成,并且这些内存段(例如java堆栈)必须是连续的才能工作


你是对的他们需要在虚拟内存中保持连续,但操作系统执行此操作。在Linux上,至少没有使用的段,只有一个32位或64位内存区域。


所以如果JVM程序只需使用一个诸如malloc的命令来获得堆内存的最大大小并将该内存划分为多个段,

堆是分成几代或G1多个内存块,但这仅用于对象。


我们没有承诺连续内存


垃圾收集器通过复制内存来对内存进行碎片整理,或者采取措施减少内存以确保您分配的任何对象具有足够的连续内存。


如果有人可以帮我在我脑海中弄出这样的话,我会很喜欢它,它们都混在一起...


简而言之,JVM像任何其他程序一样运行,除非Java代码运行时它的对象被分配在托管区域中的记忆。所有其他内存区域的行为与在C程序中的行为相同,因为JVM是一个C / C ++程序。


Alright so I have a question regarding the Memory segments of a JVM, I know every JVM would choose to implement this a little bit different yet it is an overall concept that should remain the same within all JVM's

A standart C / C++ program that does not use a virtual machine to execute during runtime has four memory segments during runtime, The Code / Stack / Heap / Data all of these memory segments are automatically allocated by the Operating System during runtime.

However, When a JVM executes a Java compiled program, during runtime it has 5 Memory segments

The Method area / Heap / Java Stacks / PC Registers / Native Stacks

My question is this, who allocates and manages those memory segments? The operating system is NOT aware of a java program running and thinks it is a part of the JVM running as a regular program on the computer, JIT compilation, Java stacks usage, these operations require run-time memory allocation, And what I'm failing to understand Is how a JVM divides it's memory into those memory segments. It is definitely not done by the Operating System, and those memory segments (for example the java stacks) must be contiguous in order to work, so if the JVM program would simply use a command such as malloc in order to receive the maximum size of heap memory and divide that memory into segments, we have no promise for contiguous memory, I would love it if someone could help me get this straight in my head, it's all mixed up...

解决方案

When the JVM starts it has hundreds if not thousand of memory regions. For example, there is a stack for every thread as well as a thread state region. There is a memory mapping for every shared library and jar. Note: Java 64-bit doesn't use segments like a 16-bit application would.

who allocates and manages those memory segments?

All memory mappings/regions are allocated by the OS.

The operating system is NOT aware of a java program running and thinks it is a part of the JVM running as a regular program on the computer,

The JVM is running as a regular program however memory allocation uses the same mechanism as a normal program would. The only difference is that in Java object allocation is managed by the JVM, but this is the only regions which work this way.

JIT compilation, Java stacks usage,

JIT compilation occurs in a normal OS thread and each Java stack is a normal thread stack.

these operations require run-time memory allocation,

It does and for the most part it uses malloc and free and map and unmap

And what I'm failing to understand Is how a JVM divides it's memory into those memory segments

It doesn't. The heap is for Java Objects only. The maximum heap for example is NOT the maximum memory usage, only the maximum amount of objects you can have at once.

It is definitely not done by the Operating System, and those memory segments (for example the java stacks) must be contiguous in order to work

You are right that they need to be continuous in virtual memory but the OS does this. On Linux at least there is no segments used, only one 32-bit or 64-bit memory region.

so if the JVM program would simply use a command such as malloc in order to receive the maximum size of heap memory and divide that memory into segments,

The heap is divided either into generations or in G1 multiple memory chunks, but this is for object only.

we have no promise for contiguous memory

The garbage collectors either defragment memory by copying it around or take steps to try to reduce it to ensure there is enough continuous memory for any object you allocate.

would love it if someone could help me get this straight in my head, it's all mixed up...

In short, the JVM runs like any other program except when Java code runs it's object are allocated in a managed region of memory. All other memory regions act just as they would in a C program, because the JVM is a C/C++ program.

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

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