Between Java and C + + there is a wall of dynamic memory allocation and garbage collection technology, people outside the wall want to go in, and the people in the wall want to come out. [From in-depth understanding of Java virtual machines]
For Java programmers, with the help of a virtual machine's automatic memory management mechanism, it is not necessary to manually free up memory for each new object or to trigger a memory leak and memory overflow problem, but once this issue is triggered, it is difficult to find a problem if we don't understand how the virtual machine uses memory.
The so-called Java memory area, mainly the runtime data region. When running a Java program, the Java Virtual machine divides the memory it manages into different regions, each with its own object and lifetime. According to the Java Virtual Machine specification 1.7, the memory is divided into these parts: program counter, Java Virtual machine stack, local method Stack, Java heap, method area. An example,
Figure a Java runtime data region
The next step is to describe the definition and role of these areas:
Program counter
The program counter is a small area of memory that is used to record where the thread is executing. Because the Java Virtual machine is multi-threaded when it is implemented by thread rotation and allocation of processor execution, it is possible to find a normal execution location when a thread resumes. This shows that this area is also thread-private, and the threads are not affected by each other.
Java Virtual Machine stack
The virtual machine stack and the program counter are also thread-private, and the lifecycle is synchronized with the owning thread. When a virtual machine executes a method, it creates a stack frame that stores information such as local variables, method exits, dynamic links, and so on. The invocation and execution of a method corresponds to the process of stacking and stacking a stack frame from a virtual machine stack.
The local variable table holds the base data type, int,boolean,byte,char,short,long,double,float, the reference to the object, these are known during compilation, and when a method is executed, The amount of memory that needs to be allocated in the stack frame is also fixed, unchanged during the execution of the method.
There are two possible exceptions to this area: Stackoverflowerror,outofmemoryerror, when a thread requests a stack depth greater than the maximum allowed depth of the virtual machine, the Stackoverflowerror exception is thrown, but the generic virtual machine dynamically expands. However, if there is not enough memory in the extension, OutOfMemoryError will be thrown.
Local method Stack
The local method stack and the virtual machine stack work Similarly, except that the local method stack serves the native method used by the virtual machine. Some virtual machines combine the local method stack with the virtual machine stack.
Java heap
Unlike the previous blocks, the Java heap is an area shared by all threads in the virtual machine and created when the virtual machine is started. For most applications, the Java heap is the largest piece of memory managed by a virtual machine. Used to hold object instances and arrays.
The Java heap is also the main area managed by the garbage collector, so it is sometimes called the GC heap (garbage collected heap). As the garbage collector is generally used in the generation of collection algorithm, all Java heap is divided into the new generation and the old age, the new generation of surviving objects are relatively small, the old age of the majority of surviving objects, garbage collection This part of the article will be described in the following articles.
In addition, the Java heap can be physically allocated in a discontinuous memory space. OutOfMemoryError are thrown when there is not enough memory to allocate an instance or array, and no longer can be extended.
Method area
The method area and the heap are also thread-shared areas for storing class information, constants, static variables, etc. that have been loaded by the virtual machine. It is described in the Java Virtual Machine specification as a logical part of the Java heap, and it is more restrictive, for example, in discrete memory spaces that can be extended or fixed (unlike virtual machine implementations). Sometimes the method area is also called a permanent generation, but data that goes into this area is recycled as well. Memory reclamation in this area is primarily for the collection of constant pools and for class unloading. However, this part of the recycling limit is more, so the efficiency of recovery is relatively low. It will also throw outofmemoryerror.
Run a constant-rate pool
Part of the method area when you run a constant pool. It is used to store a run-time pool that generates various literal and symbolic references during compilation, and the class loads the backward into the method area. The Java Virtual machine has a strict format for the class file, and each byte is used to store that data in compliance with the specification before it can be approved, loaded and executed by the virtual machine, and this part will be described in the following article.
Running a constant pool is also dynamic, and not only constants generated during compilation will be placed in the run-time pool, but it may also be possible to put the generated constants into them during the run, such as The Intern () method of the String class. In addition, it will also throw OutOfMemoryError
Direct Memory
Direct memory is not part of the Java runtime data, nor is it a memory area defined in the virtual machine specification, but it is also frequently called and sometimes throws OutOfMemoryError. After JDK1.4, a channel-and buffer-based i\o method is referenced, which allocates memory directly using the native library and then operates through a Directbytebuffer object stored in the Java heap as a reference to memory. Avoids duplication in the Java heap and native heap, improving performance.
Direct memory is not limited by the size of the Java heap memory, but it is also limited by the total memory size of the device and the processor addressing space.
Memory area of Java