简体   繁体   English

JVM内存段分配

[英]JVM Memory segments allocation

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 好吧我有一个关于JVM的内存段的问题,我知道每个JVM都会选择实现这个有点不同但它是一个整体概念,应该在所有JVM中保持相同

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. 在运行时期间不使用虚拟机执行的标准C / C ++程序在运行时期间有四个内存段,代码/堆栈/堆/数据所有这些内存段在运行时由操作系统自动分配。

However, When a JVM executes a Java compiled program, during runtime it has 5 Memory segments 但是,当JVM执行Java编译的程序时,在运行时它有5个内存段

The Method area / Heap / Java Stacks / PC Registers / Native Stacks 方法区/堆/ Java堆栈/ PC寄存器/本机堆栈

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. 操作系统不知道正在运行的java程序,并认为它是作为计算机上的常规程序运行的JVM的一部分,JIT编译,Java堆栈使用,这些操作需要运行时内存分配,以及我是什么无法理解JVM是如何将内存划分为这些内存段的。 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... 它绝对不是由操作系统完成的,那些内存段(例如java堆栈)必须是连续的才能工作,所以如果JVM程序只是使用诸如malloc之类的命令来获得最大的大小堆内存并将内存分成几段,我们没有连续记忆的承诺,如果有人可以帮我把这个直接记在脑子里,我会很高兴,这一切都混淆了......

When the JVM starts it has hundreds if not thousand of memory regions. 当JVM启动时,它有数百个甚至数千个内存区域。 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. 每个共享库和jar都有一个内存映射。 Note: Java 64-bit doesn't use segments like a 16-bit application would. 注意:Java 64位不使用像16位应用程序那样的段。

who allocates and manages those memory segments? 谁分配和管理那些内存段?

All memory mappings/regions are allocated by the OS. 所有内存映射/区域都由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, 操作系统不知道正在运行的java程序,并认为它是作为计算机上的常规程序运行的JVM的一部分,

The JVM is running as a regular program however memory allocation uses the same mechanism as a normal program would. JVM作为常规程序运行,但内存分配使用与普通程序相同的机制。 The only difference is that in Java object allocation is managed by the JVM, but this is the only regions which work this way. 唯一的区别是Java对象分配由JVM管理,但这是唯一以这种方式工作的区域。

JIT compilation, Java stacks usage, JIT编译,Java堆栈使用,

JIT compilation occurs in a normal OS thread and each Java stack is a normal thread stack. JIT编译发生在正常的OS线程中,每个Java堆栈都是普通的线程堆栈。

these operations require run-time memory allocation, 这些操作需要运行时内存分配,

It does and for the most part it uses malloc and free and map and unmap 它确实使用mallocfree以及mapunmap

And what I'm failing to understand Is how a JVM divides it's memory into those memory segments 而我无法理解的是JVM如何将其内存划分为那些内存段

It doesn't. 它没有。 The heap is for Java Objects only. 该堆仅用于Java对象。 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 它绝对不是由操作系统完成的,那些内存段(例如java堆栈)必须是连续的才能工作

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. 在Linux上至少没有使用任何段,只有一个32位或64位内存区域。

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, 因此,如果JVM程序只是使用诸如malloc之类的命令来接收堆内存的最大大小并将该内存划分为段,

The heap is divided either into generations or in G1 multiple memory chunks, but this is for object only. 堆被分为几代或G1多个内存块,但这仅用于对象。

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. 简而言之,JVM像任何其他程序一样运行,除非Java代码运行时它的对象被分配在内存的托管区域中。 All other memory regions act just as they would in a C program, because the JVM is a C/C++ program. 所有其他内存区域的行为与在C程序中的行为一样,因为JVM是C / C ++程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM