“This is the third day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

Introduction Today is the third day of the JVM, the first two days there is nothing dry, today with you liver a liver hardcore things, how hard? First press no table, today to talk about the MEMORY region of the JVM virtual machine.

Why? (according to)

As a developer who deals with JAVA every day, I think everyone will encounter the problem of stack overflow or memory overflow. When we encounter the problem, we should first check Baidu. Baidu is good, but Baidu is not? Ask the company’s high T, high T free is ok, but if not? So ah. We want to learn the students of Tsinghua University, learn their unremitting self-improvement (start brainwashing again), how to do that? We need to first understand how the virtual machine uses memory, how the memory is divided, what each part does, what problems it causes, etc. (wake up, don’t just know about heap and stack).

The data area at runtime

The JVM divides the memory it manages into several regions with different names and divisions, including:

  • Method area
  • VM stack
  • Native Method Stack
  • heap
  • Program Counter register

PC register

The program counter, is derived from the program was the first to register, in JAVA bytecode interpreter can be performed by changing its value needs to execute bytecode instruction, not only so, our program control, branches, loops, jumps, exception handling, thread restore all need to rely on it to complete, ha ha ha is very strong, don’t you think it is a count.

Due to a processor can only execute at the same time a thread’s instruction, the execution after this one, how to ensure that the processor is when switching back to the execution of the original position, it must have something to remember this place, that’s right, this living liver by the program counter, Java support multithreading, so give each thread sends a younger brother, You just count for me, and then you tell the processor where it’s going. The technical term is thread private.

The Java Vm Specification states that if the thread is executing a Java method, the program counter is the address of the method instruction being executed, and if the method is a native method, the counter value is undefiend.

VM stack

Like the program counter, it is thread private and has the same lifetime as the thread. For each method, the thread creates a Stack Frame for each method, containing information such as a local variable table, a Stack of operands, dynamic links, and method exits. Simply put: the execution and end of a method is a process of pushing and off the stack, you walk into the supermarket, start to consume, come out, consume.

// Todo local variable table, stack frame first press not table

If the Stack depth of the thread is greater than that of the virtual machine, throw Stack Over flow error. PS: there will be no OOM if the thread has successfully applied for the stack, otherwise it will.

Native Method Stack

As the name suggests, any local method plays here.

The Java Virtual Machine specification explains: there are no clear restrictions on the language, mode, and data structure of the native methods, which can be implemented as needed. But the local method Stack also throws a Stack Over flow error and out of Memory error, respectively, when the Stack depth overflows or when the expansion fails

Heap

The heap is an area shared by all threads and created when a virtual machine starts. In Java, this is the area used to store objects, so it is the largest area. Confessed: “code for the Java virtual machine all objects and arrays should be on the heap allocation, but because the Java development for many years, especially the instantaneous compiling technology advances, more optimization to cause on the Java heap allocation is not so absolute, escape analysis, scalar (PS: instantaneous compiling replacement, stack allocated on first press the table).

Java Heap is also an area of memory managed by garbage collection, which is called GC, and you’ve definitely heard this before, but I would say that garbage collection is partly based on the idea of generational collection, not entirely, and there are new garbage collectors out there that don’t use generational collection.

TLAB is a thread private allocation buffer, which can improve the efficiency of object allocation. (Click not to see the table first. No matter how the Java heap is subdivided, one thing is constant: faster object allocation, faster garbage handling (for details, click not to table first).

The Java Virtual Machine Specification states that the Java heap is a discontinuous area of memory, that the Java heap is extensible (-xMX-XMS), and that an out of Memory error is thrown when the Java heap has no memory for instance allocation and that the heap cannot be expanded.

Method erea

Like the Java heap, an area shared by threads to store type information loaded by the VM, constants, dynamic variables, compiled code data, etc., and separately named non-heap.

The Java Virtual Machine Specification states that an out of Memory error will be thrown if the method area does not meet the new memory allocation.

Runtime Constant Pool

Constant pools are used to hold a variety of literal and symbolic references, most commonly strings

The Java Virtual Machine Specification states that an out of Memory error will be thrown if the constant pool does not meet a new memory allocation.

Digression: Today tired and full!