Java Automatic Memory Management Mechanism

Source: Internet
Author: User
Tags xms

Chapter 2: Java memory area the java Virtual Machine divides the memory it manages into many different data areas during Java program execution. These regions have their own purposes, as well as the creation and destruction time. Some regions exist with the startup of virtual machine processes, some regions are created and destroyed Based on the startup and termination of the user thread. In the Java Virtual Machine specification, the memory managed by the Java Virtual Machine is divided into the following areas. 1. Program Counter Register a Program Counter is a small memory space and its role is the row number indicator of the currently executed bytecode. It is private to the thread, that is, each thread has an independent program counter. If the thread is executing a java method, this counter records the address of the Virtual Machine bytecode instruction being executed; if the native method is being executed, this counter is undefined ). This memory region is the only region that does not throw an OutOfMemoryError exception. Ii. Java Virtual Machine Stacks (java Virtual Machine Stacks) we may often hear that Java memory is divided into heap memory and stack memory. In fact, stack memory refers to the partial variable table in the java Virtual Machine stack. The java Virtual Machine Stack describes the Memory Model of java method execution: A Stack Frame is created when each method is executed ), stores information about local variable tables, Operation stacks, dynamic links, and method exits. Each method is called until the execution is completed, which corresponds to the process from the inbound stack to the outbound stack of a stack frame in the Virtual Machine stack. The Java Virtual Machine stack is also thread-specific and has the same lifecycle. There are three types of local variable tables: 1. The basic data types known during compilation: boolean, byte, char, short, int, float, long, and double; 2. Object reference: The reference type. It stores a reference pointer pointing to the starting address of the object in the heap, a handle representing the object, or other locations related to the object, different implementations vary depending on the Virtual Machine. 3. returnAddress type: stores the address pointing to a bytecode instruction. The memory space required for the local variable table is allocated during compilation, when you enter a method, the size of the local variable tablespace that needs to be allocated in the frame is completely determined. The method does not change the size of the local variable table during running. Java Virtual Machine stack throws two types of exceptions: 1. OutOfMemoryError: If the virtual machine can be dynamically expanded, it will be thrown when the extension cannot be applied for enough memory; 2. StackOverflowError: if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, it is thrown. 3. The local Method stack (Native Method Stacks) is similar to the Virtual Machine stack, it serves virtual machines when they execute the native method, while the Virtual Machine stack provides java method services for virtual machines. The OutOfMemoryError and StackOverflowError exceptions are also thrown in this memory area. 4. java Heap Java Heap is used to store object instances and arrays. It is the largest part of memory managed by Java Virtual Machine and shared by all threads. It is created when the virtual machine is started and is also the necessary area for managing the garbage collector, almost all object instances are allocated memory here. If the Garbage Collector uses generational collection, it can also be divided into the new generation and the old generation, and there are more detailed Enden space, From nation vor space, To other vor space. The java heap can process physically discontinuous memory space as long as it is logically continuous. In implementation, you can achieve a fixed or scalable value. If it is extensible, you can use-Xms and-Xmx to specify the minimum and maximum values. If-Xms and-Xmx are of the same value, it is equivalent to non-expandable. If the heap does not have memory for instance allocation, an OutOfMemoryError error occurs in this memory area. 5. The method area is used to store information about classes loaded by virtual machines, constants, static variables, and Code Compiled by the real-time compiler. It is a logical part of the heap and a memory area shared by each thread. If it is sun's HotSpot virtual machine, it is also called Permanent Generation. If the method area cannot meet the memory allocation requirements, an OutOfMemoryError exception is thrown. The RuntimeConstant Pool in the method area is used to store various literal and symbolic references generated during the compilation period. It is stored in the runtime Pool after the class is loaded. The runtime constant pool is dynamic, that is, constants are not necessarily generated only during the compilation period. During the runtime, new constants may also be stored in the pool, such as the intern () method of the String class. 6. Direct Memory (DirectMemory) is not part of the data zone during virtual machine running hours, nor is it the memory zone defined in Java Virtual Machine specifications. For example, the NIO class added in JDK1.4 has an I/O Method Based on channel and buffer. It can use the native function library to directly allocate off-heap memory, then, a DirectByteBuffer object stored in the java heap is used as a reference to the memory. Therefore, the direct memory will not be limited by the java heap size, but will be limited by the total memory size of the local machine and the addressing space of the processor. It also throws an OutOfMemoryError.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.