JVM memory Management in Java (1)

Source: Internet
Author: User
Tags xms

Java job interview, the JVM is the basic skills of the programmer to investigate, usually ask you to understand the JVM? You can answer this question in a few parts, first JVM memory Partitioning |  What the JVM garbage collection means | What are the GC algorithms and the characteristics of the younger generation and the old age?

 1) JVM Memory Partitioning:

① method Area (thread shared) constant static variable JIT (instant compiler) compiled code is also stored in the method area

② heap memory (thread sharing) The main site of garbage collection

③ position indicator of bytecode executed by the current thread of the program counter

④java Virtual machine stack (stack memory): Save local variables, base data types, and reference variables for objects in heap memory

⑤ Local method Stack (c stack): Provides services to the JVM using the native method

Take a look at this picture

JDK 1.8 with JDK 1.7, the biggest difference is: Metadata replaces the permanent generation. The nature of the meta-space is similar to the permanent generation, which is the implementation of the method area in the JVM specification. The biggest difference between the meta-space and the permanent generation is that the metadata space is not in the virtual machine, but in local memory

Learn more about each section

01) Program counter (PC Register)

    program Counter definition: The program counter is a small amount of memory space, which is the current thread is executing the address of the bytecode instruction, if the current thread is executing a local method, then the program counter is undefined

    The function of the program counter:

      • Bytecode interpreter to control the process of code by changing the program counter to get the instruction sequentially
      • In multithreaded situations, the program counter records where the current thread executes, so that when the thread switches back, it knows where the last thread was executed.

    Features of the program counter

      • is a small piece of memory space
      • Thread-Private, each thread has its own program counter
      • Life cycle: Created as threads are created, destroyed as threads are destroyed
      • is the only memory area of the outofmemoryerror that does not appear

  Java Virtual Machine stack

    definition: A memory model that describes the process in which Java methods run

The Java Virtual machine stack creates an area called a "stack frame" for each Java method that is about to run, storing some of the information that the method is running, such as the local variable table/operand stack/dynamic link/method export information ....

    

    Stack stack process:

When a local variable needs to be created during a method's run, the value of the local variable is stored in the local variable table of the stack frame

The stack top of the Java Virtual machine stack is currently executing the activity stack, that is, the currently executing method, the PC register will point to this address, only the active stack frame local variables can be manipulated by the stack operation, the current stack frame calls another method, and the corresponding stack frame will be created, The newly created stack frame presses into the top of the stack, becomes the current active stack frame, and after the method ends, the return value of the current stack frame becomes an operand of the operand stack in the new active stack frame, and if there is no return value, the operand of the operand stack in the new active stack frame does not change

Because the Java Virtual machine stack is thread-appropriate, the data is not shared, so there is no need to care about data consistency issues, and there is no synchronization lock issue

    Characteristics

      • The local variable table is created with the creation of the stack frame, and his size is determined at compile time by assigning a predetermined size only, and the size of the local change table will not change during the method's operation.
      • There are two exceptions to the Java Virtual machine stack: Stackoverflowerror and OutOfMemoryError
      • Stackoverflowerror if the size of the Java Virtual machine stack does not allow for dynamic scaling, this exception is thrown when the depth of the stack of the front-thread request exceeds the maximum depth of the current Java Virtual machine stack
      • OutOfMemoryError, if dynamic scaling is allowed, this exception is thrown when the stack memory of the front-thread request is exhausted and can no longer be dynamically extended.
      • The Java Virtual machine stack is also thread-private, created with thread creation and destroyed as the thread ends

  03) Local method Stack (c stack)

    definition: is for the JVM to run the native method prepared space, because many native methods are implemented in C language, it is usually called C-stack, it is similar to the Java Virtual machine stack implementation, but the local method stack describes the local method of running the memory model of the process

    Stack frame Change Process:

When the local method is executed, a stack frame is also created on the local method stack for storing the local variable table/operand stack/dynamic link/Method exit of the method; At the end of the method, the corresponding stack frame will also stack up and free up memory space. Stackoverflowerror and OutOfMemoryError exceptions are also thrown

  04) Heap

    definition: heap is the memory space used for objects, and almost all objects are stored in the heap

    Characteristics:

      • Thread sharing, the entire Java Virtual machine has only one heap, and all threads access the same heap.
      • Created at virtual machine startup
      • is the main site of garbage collection
      • Further divided into: The New generation (Eden zone from the Survior to Surviror) The old age

Different areas of the storage of different life cycle objects, which can be based on different areas of the use of different garbage collection algorithm, more targeted. The heap size can also be fixed or extensible, and for mainstream virtual machines, the heap size is extensible, so when a thread requests the allocated memory but the heap is full and the memory is no longer expandable, the OutOfMemoryError exception is thrown

  05) Method Area

    Definition: The Java Virtual machine specification defines the method area as a logical part of the heap, where the following information has been loaded by the virtual machine for class information/constants/static variables/immediately compiled code

    Characteristics:

      • Thread sharing. The method area is a logical part of the heap, so, like a heap, it is thread-shared, with only one method area in the entire virtual machine
      • The information in the permanent generation method area usually needs long-term existence, and it is the logical partition of the heap, so the method area is called "permanent generation" by the method of heap partitioning.
      • Memory recovery is inefficient. The information in the method area generally needs to exist for a long time, and only a small amount of information is not valid for recycling. The main purpose of the recovery is to recycle the constant pool; type unloading
      • Java Virtual Machine Specification L The requirements for the method area are looser, as is the heap, which allows for a fixed size. also allows dynamic scaling, allowing for no garbage collection

    To run a constant-rate pool:

Stored in the method area: class information constant static variable the immediate compiler becomes the compiled code. Constants are stored in the run-time constant pool. When a class is loaded by a Java virtual machine, the constants in the. class file exist in the method area, and during the run, new constants can be added to the constant pool. such as The Intern () method of the String class can add string constants to a constant pool during run time

  06) Direct memory (out-of-heap memory)

Direct memory is memory other than the Java virtual machine, but it may be used by Java

  To manipulate Direct memory:

A channel-and cache-based IO approach was introduced in NiO, where he could call the local method to directly allocate memory outside of the Java virtual machine, and then manipulate the memory directly through a Directbytebuffer object stored in the heap without having to copy the external in-memory data into the heap. To improve the efficiency of data operations, the size of the direct memory is not affected by the Java Virtual machine, will also throw OutOfMemoryError exception

  Direct memory and heap memory comparisons

    • Direct Memory application space consumes higher performance
    • Direct memory Read IO has better performance than normal heap memory
    • Direct Memory chain: Local io--> Direct Memory---local IO
    • Heap Memory action chain: Local io--> Direct Memory---non-direct memory--Direct Memory---> Local io
When the server administrator configures the virtual machine parameters, the parameters such as-XMX are set according to the actual memory,  but the direct memory is often ignored, so that each
The sum of memory areas is greater than physical memory, resulting in dynamic scaling when outofmemoryerror occurs

 2) similar to-xms-xmn the meaning of these parameters

   Heap memory allocation

①: The initial memory allocated by the JVM is specified by-XMS, which defaults to 1/64 of the physical memory

②: The maximum allocated memory for the JVM is specified by-XMX, which defaults to 1/4 of the physical memory

③: When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS

④: So the server is generally set-XMS-XMX equal to avoid resizing the heap after each GC. The heap memory of an object is reclaimed by an automatic memory management system that becomes the garbage collector

   Non-heap memory allocations:

①:JVM uses-xx:permsize to set the initial value of non-heap memory, which is 1/64 of the default physical memory;

②: Setting the maximum non-heap memory size by the Xx:maxpermsize setting

③:-xmn2g: Set the size of the young generation to 2G

④:-xx:survivorratio, setting the ratio between Eden and survivor in the young generation

3) What are the algorithms for garbage collection?

① Reference Counting Method: The principle is that there is a reference to this object, that is, adding a count and deleting a reference decreases one count. When garbage collection, only objects with a count of 0 are collected. The most deadly problem with this algorithm is that it cannot handle circular references

②: Mark-clear: This algorithm is divided into two phases, the first phase marks all referenced objects starting from the root node of the reference, the second stage traverses the entire heap, clears the Unlabeled object, and the algorithm needs to pause the application while generating memory fragmentation

③: Copy algorithm This algorithm divides memory into two equal regions, using only one region at a time, garbage collection, traversing the currently used area, copying the object in use to another region each time the algorithm only processes the object being used, so the cost of replication is smaller, At the same time copy the past can also be a corresponding memory collation, no "fragmentation problem", the shortcomings of this algorithm is also obvious, need twice times the memory space

④: Tagging-collation: This algorithm combines the advantages of "mark-clear" and: two of the copy algorithm, is also divided into two phases, the first stage from the root node to mark all referenced objects, the second stage to traverse the entire heap, clear unmarked objects and "compress" the surviving objects into one of the heap, in order to discharge, This algorithm avoids the "mark-erase" fragmentation problem and also avoids "copying" the space problem

4) Root search algorithm, which can be used as root?

    • Classes loaded by the startup class (bootstrap loader) and objects created
    • Objects referenced in Javastack (objects referenced in stack memory)
    • Static reference in method area

JVM memory Management in Java (1)

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.