Java multithreaded - multithreading understanding from the perspective of JVM

In the JVM, multiple threads share the process of heap and method area resources, but each thread has its own program counter , the virtual machine stack and native method stacks

What is the heap

The largest piece of memory management in the Java virtual machine, java heap is shared by all threads to a memory area is created when the virtual machine starts, used to store an object instance, almost all object instances and arrays are all here to allocate memory .

Java heap is the main area managed by the garbage collector, and therefore also referred to as GC heap (Garbage Collected Heap) . From the perspective of garbage collection, since the collector is now basically using generational garbage collection algorithm, so Java stack also be subdivided It is: the new generation and the old year: there are again more carefully: Eden space, From Survivor, to Survivor space. Further divided in order to better recovery of memory, allocating memory or faster.

Eden region shown above, s0 region, s1 belongs to the new generation area, tentired area belongs year old. In most cases, the object will be first in the Eden area allocation, after a new generation garbage collection, if the object is still alive, will enter s0 or s1, and age of the subject will add 1 (Eden area -> after Survivor target area initial Age becomes 1), when its age to a certain extent (the default is 15 years old), will be promoted to the old era. Objects promoted to the age threshold of old age, the parameters can -XX:MaxTenuringThresholdbe set.

What is the method area

The method area and the Java heap, as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data.

Meanwhile runtime constant pool is also part of the method area. Known that, when using string assignment (non new new), and packaging Byte, Short, Integer, Long, Character, Boolean assignments within the range of -128 to 127

What is a virtual machine stack

Java memory can be divided into a rough heap (Heap) and stack memory (Stack), which stack is now talking about a virtual machine stack, or a virtual machine stack local variable table section. (In fact, Java virtual machine stack is a stack frames, but each has its stack frame: the local variable table, the operand stack, dynamic linking method export)

Local variable table to store various data types main compiler knowable (boolean, byte, char, short , int, float, long, double), object reference (reference type, which is different from the object itself, the object may be a starting point It references the start address pointer, a point may be representative of a handle or other object associated with this position of the object).

What is a native method stacks

And virtual machines stack the role very similar, the difference is: VM stack for the Java virtual machine to perform method (ie bytecode) services, native method stacks, the virtual machine to use a method of Native service. In the HotSpot virtual machine and the Java virtual machine stacks combined.

When the method is performed locally, the stack will also create a local stack frame method, method for storing the local variables of the local table, the operand stack, dynamic link, the information outlet.

What is the program counter

The program counter is a smaller memory space can be seen as an indicator byte-code line number of the current thread of execution. By changing the bytecode interpreter is the value of the counter to select the next bytecode instruction to be executed, a branch, looping, branching, exception handling, thread recovery functions need to rely on this counter to complete.

In addition, in order to restore the thread switch to the correct execution position, each thread requires a separate program counter, independently of each other between the threads, independent store, we call this type of memory area for the "thread private" RAM.

Ref:

  1. Java memory area may be the talk of the clearest article

Guess you like

Origin juejin.im/post/5d03b2ae51882546dd100b04
Recommended