In C/C ++ development, you need to manage the memory yourself. Java, with the help of the Automatic Memory Management Mechanism of JVM, no need to write the paired Delete and free code for each new operation. However, due to memory leakage and benefits, This section summarizes some knowledge in the Java memory area.
The data zone when the Java virtual machine is running is as follows:
It consists of five parts: Program counters, Java Virtual Machine stacks, local method stacks, Java stacks, and method zones (runtime frequent pools ).
(1) program counter (thread private ):
The program counter is a small memory space. Its function can be viewed as the row number indicator of the bytecode executed by the current thread. Such as the execution of bytecode commands, branches, loops, jumps, exception handling, thread recovery, and other basic functions.
JVM supports multiple threads at a time. Each JVM thread has its own program counter. At any time, a JVM thread is executing the method code of the current thread. If this method is a Java method, the program counter records the address of the Virtual Machine bytecode instruction being executed. If the thread is executing a local method, the program counter value is undefined. The JVM program counter is sufficient to store a return address or a local pointer.
(2) Java Virtual Machine stack (thread private ):
Each JVM thread creates a stack when it is created. A stack contains many stacks. Stack frames are used to store local variable tables (various basic data types of the compiler are stored, such as Boolean, byte, Char, short, Int, float, long, and double; object Reference (for example, the reference pointer pointing to the starting address of the object or the handle representing the object), returnaddress type, Operation stack, dynamic link, method exit, and other information of different virtual machine storage.
In the Java Virtual Machine specification, two exception conditions are specified for this region:
A. If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, an stackoverflowerror error is thrown.
B. If the VM stack can be dynamically expanded, an outofmemoryerror error will be thrown when the VM stack cannot be applied for sufficient memory (or there is not enough stack creation when a new thread is initialized.
(3) Local method Stack:
The role of the local method stack is similar to that of the Java Virtual Machine stack. The Java Virtual Machine stack serves to execute the Java method (also known as bytecode), and the local method stack serves the native method used by the virtual machine.
In the Java Virtual Machine specification, two exception conditions are specified for this region:
A. If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, an stackoverflowerror error is thrown.
B. If the VM stack can be dynamically expanded, an outofmemoryerror error will be thrown when the VM stack cannot be applied for sufficient memory (or there is not enough stack creation when a new thread is initialized.
(4) Java heap
JVM has a heap shared among all threads. Heap is the runtime data zone that allocates memory to instances and arrays of all classes. The heap is created when the VM is started. objects stored in the heap are recycled by an Automatic Storage Management System (Garbage Collector. Objects are not explicitly allocated (JVM never specifies the release of objects ). Add a special type of Automatic Storage Management System (the JVM does not specify a specific Automatic Storage Management System) to the JVM. (developers can choose one based on system requirements) in addition, this storage management technology may be chosen to meet the system requirements.
Java heap is the main area of the garbage collector management, also known as the "GC Heap ".
The heap size is either fixed or expanded as needed. If a large heap becomes redundant, it may contract. The heap memory does not need to be adjacent. You can set the heap memory size if the heap can be dynamically expanded. Controls the maximum and minimum heap memory.
The following exceptions occur in the heap:
If the memory overflows (if the heap memory required for calculation is insufficient), an outofmemoryerror is thrown.
(5) Method Area
The JVM method area is shared by all threads. The method area is similar to the storage area when code is compiled in a traditional language or text segment similar to an operating system process. Its Storage content includes: the structure of each class, such as the runtime constant pool, field and method data; the Code of the method and constructor, such as for the class, special methods for instance and interface initialization. This method area is created when the JVM starts. Generally, the JVM does not choose to recycle or compress the method area, the JVM specification of this version does not specify the location of the Method Area and the policy for managing compiled code. The method area can be fixed or scaled as needed. The memory in the method area does not need to be adjacent.
Runtime constant pool(Part of the Method Area ):
The runtime constant pool is a constant pool table for classes and interfaces. It is in a bytecode file. It contains several constants. A numerical constant identified during compilation, a method recognized in the runtime or a reference field. The constant pool in the runtime zone is similar to the two tables in traditional languages, but it is more extensive than the traditional two tables. The constant pool in each running zone allocates memory from the method zone. When classes and interfaces are created by JVM, the corresponding constant pool is also created.
The constant pool in the runtime zone contains the following exceptions:
When a class or interface is created, if the memory required by the constant pool in the runtime zone is insufficient, an outofmemoryerror is thrown.