6

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...

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
DrPrItay
  • 793
  • 6
  • 20

1 Answers1

5

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    Good answer, I also wanted to write one, but was not sure how to formulate it exactly. In short, your last sentence sums up what I would have said: `All other memory regions act just as they would in a C program, because the JVM is a C/C++ program.` – Markus Weninger Feb 21 '16 at 10:16
  • Firstly, thank you very very very very much, this really helped me understanding a lot!, however I've still failed understanding one thing, I've read in many places that java stacks are not necessarily thread stacks that are implemented with contiguous memory, could you please explain that to me? – DrPrItay Feb 26 '16 at 11:22
  • @DrPrItay which "one thing" is that? – Peter Lawrey Feb 26 '16 at 11:23
  • @DrPrItay I can't see what you have changed. – Peter Lawrey Feb 26 '16 at 11:29
  • @PeterLawrey ahh My question was I've read in many places that java stacks are not necessarily thread stacks that are implemented with contiguous memory, A regular stack such as of a C/C++ program would be contiguous, in java I've read stack frames are implemented a little bit different – DrPrItay Feb 26 '16 at 17:05
  • @DrPrItay Java stacks, as they are seen in byte-code are different in the sense that they are not literally based on the space they use. However, at runtime, the HotSpot JVM uses regular native stacks to implement this. – Peter Lawrey Feb 27 '16 at 10:21
  • @PeterLawrey this is really good and precise explanation. Thank you so much. I have been struggling with these questions for long long time. I have a question on this. While executing java program, only heap is managed by JVM and other segments like stack, data etc are managed by OS, then how does JVM handles 'Method' calls present in bytecode i.e. in java code. Because method calling/handling requires stack segment and OS is no aware of java method, it is only aware of JVM method call written in C code. Please help me with this question! – Ankush G Aug 16 '17 at 06:57