1 Java Virtual machine-to-memory management Java virtual machines divide memory into a number of different zones when executing Java programs, each of which has its own different uses, as well as the creation and destruction times. Some zones start with the start of a virtual machine, Some zones start and end depending on the start and end of the user thread. According to the Java Virtual Machine specification, the Java Virtual machine divides memory into the following sections: The following is an introduction to these areas 1 program Counter Register) program counter is a small amount of memory space, and its role can be seen as the current thread execution of the byte code signal indicator. The bytecode interpreter selects the next byte-code instruction that needs to be executed by changing the value of the counter, and the basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on, are all dependent on counters to be completed. Each JVM thread has its own program counter, and the counters between the threads do not affect each other and are stored independently, ensuring that the thread can be switched back to the correct execution location. At any point, a JVM thread executes only one method of code. The method is called the current method of the thread, and if the method is a Java method, the counter holds the address of the bytecode instruction that the JVM is executing, and if the method is native, the value of the PC register is empty (Undefined). This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions. 2 Java Virtual machine stack Java VM stack is the same thread-private as the program counter, with the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each time a method is executed, a stack frame is created to store the local variable table, the operand stack, the dynamic link, the method exit, and so on. Each method is called until the completion process corresponds to a stack frame in the virtual machine stack from the stack to the stack. Local variable tables contain the various basic data types that are known at compile time (Boolean, Byte, char, short, int, float, long, double), Object references (reference types) and ReturnAddress types (addresses that point to a bytecode directive). Where a long and double of 64 bits takes up 2 local variable space (slots), the remaining data types occupy only 1. The space required for a local variable table is allocated during compilation, and when a method is entered, it needs to be in the frameThe amount of local variable space allocated is determined, and the size of the local variable table is not changed during the run of the method. The Java Virtual Machine specification provides two exceptions for this zone: 1) If the depth of the thread request is greater than the depth allowed by the virtual machine, the Stackoverflowerror exception is thrown. 2) virtual machine stack Dynamic expansion cannot request enough memory to throw a OutOfMemoryError exception. 3 Local method Stack java virtual machines may use traditional stacks to support the execution of Native methods (methods written in languages other than the Java language), This stack is the local method stack (Native). The local method stack is very similar to the virtual machine stack, except that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, and the second local method stack is the native method service used by the virtual machine. Virtual machine Specification The method in the local method stack is not mandatory with the language, usage and data structure, so the virtual machine can be implemented freely, such as the Sun hotspot virtual machine directly merge the local method stack and virtual machine stack. The java Virtual Machine specification provides two exceptions for this zone: 1) If the depth of the thread request is greater than the depth allowed by the virtual machine, throwing a stackoverflowerror exception. 2) virtual machine stack Dynamic expansion cannot request enough memory to throw a OutOfMemoryError exception. The 4 Java heap (Java heap) Java heap is the largest chunk of Java Virtual Machine Management memory and is an area of memory shared by all threads, created with the startup of the virtual machine. The only purpose of the zone is to hold object instances, and almost all instances of objects are allocated within the heap. The Java heap is the primary area of garbage collector management, known as the GC heap. The Java Virtual Machine specification stipulates that the Java heap can be physically and physically discontinuous in memory space, as long as it is logically contiguous, as with disk space, and can be either fixed size or extended. Current mainstream virtual machines are implemented according to extensions (via-XMX and-xms control). The OutOfMemoryError exception is specified in the Java Virtual Machine specification for this zone: if there is no memory complete instance allocation in the heap and the heap cannot be extendedThe OutOfMemoryError exception is thrown. The 5 method area method zone, like the Java heap, is an area of memory shared by each thread, used to store data such as class information, constants, static variables, and instant compiler-compiled code that are loaded by a virtual machine. Java virtual machines have a very loose limit on this area, and as with Java pairs, there is no need for contiguous memory and the ability to choose a fixed or extensible size, or to opt out of garbage collection. The OutOfMemoryError exception is specified in the java virtual machine Specification: if the memory space of the method area does not meet the memory allocation request, The Java Virtual machine will throw a OutOfMemoryError exception. Note: Running a constant-time pool (runtime Constant pool) running a constant-rate pool is part of the method area. In addition to the class file in the version, fields, methods, interfaces and other information, there is also a constant pool of information, used to hold the compilation period generated by a variety of literal constants and symbol references, which are stored in the method area after the class loaded into the constant pool. The OutOfMemoryError exception is specified in the java virtual machine Specification: when the constant pool fails to request memory. OutOfMemoryError exception is thrown. Direct Memory Direct memory is not part of the data region of the virtual runtime, nor is it a memory area defined in the Java Virtual specification, but this portion of memory is also used frequently and can cause outofmemoryerror anomalies to occur. The Java virtual machine needs to set the-XMX parameter information according to the actual memory size, if the direct memory is ignored, the sum of each memory area is greater than the physical memory limit, which causes the OutOfMemoryError exception to be thrown when dynamically expanding. A code that describes the association between the Java stack, the Java heap, and the three most important memory areas of the method area Object obj = new object (); Suppose this code appears in the method body: 1) The semantics of the part "Object obj" are reflected in the local variable table of the Java stack and appear as a reference type data. 2) The semantics of the section "New Object ()" is reflected in the Java heap, forming a piece of structured memory that stores all instance data values of type object, and finding address information for this object type. 3) OBJThe type data for the ECT class (such as Object type, parent class, interface implementation, method, and so on) is stored in the method area.
1 Java memory area and memory overflow exception