Java memory Management and GC mechanism (i)

Source: Internet
Author: User

Computer CPU running faster and faster, hard disk can not keep up with the CPU read and write speed, design memory. As the CPU grows, the memory reads and writes faster than the CPU, adding a cache to each CPU. In multiprocessor systems, each processor has its own cache and shared memory (main storage), which has a cache consistency problem. Therefore, some protocols are introduced to ensure the consistency of the data when each processor is running.

  

  

In Java, multiple tasks are processed at the same time through multithreading, and all threads share the JVM memory area, and each thread has its own working memory, and when the thread interacts with the memory area, the data is copied from the main storage to the working memory, which is then processed by the thread (opcode + operand). The JVM memory structure is as follows.

  

, the JVM mainly consists of two subsystems and two components. The two subsystems are the class loader subsystem and the execution engine (execution engine) subsystem, and the two components are the runtime data area (run time) components and the native interface (local interface) components respectively.

The function of the class loader subsystem is to load the contents of a class file into the method area in the Runtime data zone, given a fully qualified name Class name (such as Java.lang.Object). Java programmers can extends Java.lang.ClassLoader classes to write their own class loader.

Execution Engine SUBSYSTEM: Executes the instructions in the classes. The core of any JVM specification implementation (JDK) is the execution engine, and the different JDK such as Sun's JDK and IBM's JDK are largely dependent on the quality of their respective execution engine implementations.

Native interface component: interacts with Native libraries and is an interface for other programming language interactions. When the native method is called, it enters a completely new world that is no longer restricted by the virtual machine, so it is also easy to have native heap OutOfMemory that the JVM cannot control.

Let's take a look at what each part does.

1. Program counter

Program Counter Register is a small memory space that acts as a line number indicator of the bytecode that is being executed by the current thread . In the virtual machine concept model (only the conceptual model, the various virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception handling, Basic functions such as thread recovery need to rely on this counter to complete. Because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order to recover the thread after switching to the correct execution location, each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this type of memory area is "thread-private" memory. if the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null (Undefined) If the Natvie method is being executed. 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

Like the program counter, the Java Virtual machine stack (Java Stacks) is also thread-private, with the same life cycle as the thread . The virtual machine stack describes the memory model that is executed by the Java method: Each method is executed with a stack frame (stack Frame①) for storing local variable tables, operation stacks, dynamic links, method exits, and so on . Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.

The Local variables table holds the various basic data types (Boolean, Byte, char, short, int, float, long, double), object references (reference types, which are not equivalent to the object itself and are implemented according to different virtual machines). It may be a reference pointer to the start address of the object, or it may point to a handle representing the object or other location associated with the object, and the ReturnAddress type (the address of a bytecode directive). The 64-bit length of long and double data takes up 2 local variable space (slots), and the remaining data types occupy only 1. The amount of memory space required for a local variable table is allocated during compilation , and when entering a method, the method needs to allocate much of the local variable space in the frame is fully deterministic and does not change the size of the local variable table while the method is running. In the Java Virtual Machine specification, there are two exceptions to this area : If the thread requests a stack depth greater than the virtual machine allows, the Stackoverflowerror exception is thrown If the virtual machine stack can be dynamically extended (most of the current Java virtual machines can be dynamically extended, but the Java Virtual Machine specification also allows a fixed-length virtual machine stack), the OutOfMemoryError exception is thrown when the extension fails to request enough memory.

3. Local Method Stack

The local methods Stack (Native method Stacks) 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, while the local method stack serves the Native method used by the virtual machine. The language, usage, and data structure of the methods used in the local method stack in the virtual machine specification are not mandatory, so the virtual machine can implement it freely. Even some virtual machines, such as sun HotSpot virtual machines, combine the local method stack and the virtual machine stack directly. As with virtual machine stacks, the local method stack area throws Stackoverflowerror and OutOfMemoryError exceptions.

4. Java Heap

For most applications, the Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated memory. If from the perspective of memory recycling, because the collector is now basically used in the generation of the collection algorithm, so the Java heap can also be subdivided into: the new generation and the old age, the more detailed Eden space, from Survivor space, to Survivor space. If viewed from a memory allocation perspective, the thread-shared Java heap may divide multiple thread-private allocation buffers (thread localallocation buffer,tlab). However, regardless of the partitioning, it is not the content of the storage, no matter what area, stored is still an object instance, the purpose of further partitioning is to better reclaim memory, or to allocate memory more quickly.

According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, just like our disk space. When implemented, it can be either fixed or extensible, although the current mainstream virtual machines are implemented in a scalable way (via-XMX and-xms control). A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.

5. Method area

The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine . Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has an alias called Non-heap (Not a heap), which should be distinguished from the Java heap. For developers who are accustomed to developing and deploying programs on a hotspot virtual machine, many people are willing to call the method area "permanent generation" (Permanent Generation), which is essentially not equivalent, just because the design team of the hotspot virtual machine chooses to put the GC The generational collection is extended to the method area, or the method area is implemented using a permanent generation. For other virtual machines (such as Bea JRockit, IBM J9, etc.) there is no concept of a permanent generation. Even the hotspot virtual machine itself, according to the official route map information, now also has to abandon the permanent generation and "move" to native Memory to achieve the planning of the method area.

The Java Virtual Machine specification has a very loose limit on this area, and you can choose not to implement garbage collection, except that you do not need contiguous memory and can choose a fixed size or extensible, as with the Java heap. The garbage collection behavior is relatively rare in this area, but it is not the data that enters the method area as "permanent" as the name of the permanent generation. The memory recovery target of this area is mainly for the recovery of constant pool and unloading of type, in general, the recovery "score" in this area is more difficult to be satisfied, especially the type of unloading, the condition is very harsh, but the recovery of this part of area is really necessary. In the Sun's bug list, several serious bugs that have been made are the memory leaks caused by the low-release hotspot virtual machine being completely reclaimed for this zone. According to the Java Virtual Machine specification, a OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

6. Running a constant-rate pool

The runtime Constant pool is part of the method area. In addition to the class file, such as the version, field, method, interface, and other information, there is a constant pool (Constant

Table), which holds the various literal and symbolic references generated during the compilation period, which are stored in the run-time pool of the method area after the class is loaded.

Java virtual machines have strict specifications for each part of a class file (which naturally includes Chang), and each byte is used to store which data must conform to the requirements of the specification, so that it is recognized, loaded, and executed by the virtual machine. However, for running a constant pool, the Java virtual machine specification does not require any details, and the virtual machines implemented by different providers can implement this memory area as they want. However, in general, in addition to saving the symbolic references described in the class file, the translated direct references are also stored in the run-time ① pool.

Another important feature of running a constant pool with respect to the regular pool of class files is the dynamic nature of the Java language

Words do not require constants to be generated only at compile time, that is, the contents of a constant pool that is not pre-built into a class file

To enter the method area to run the constant pool, and the new constants may be put into the pool during the run, this feature is developed

The Intern () method of the string class is used by more people.

Since the run-time constant pool is part of the method area, it is naturally limited by the method area memory, when the constant pool is not

The OutOfMemoryError exception is thrown when the method is re-applied to memory

---restore content ends---

---restore content ends---

---restore content ends---

---restore content ends---

---restore content ends---

---restore content ends---

---restore content ends---

---restore content ends---

---restore content ends---

Java memory Management and GC mechanism (i)

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.