The mechanism of JVM memory management

Source: Internet
Author: User

1.JVM Memory management mechanism

The memory space is divided into: the Sun JDK complies with the JVM specification when implemented, dividing the memory space into heaps, JVM method stacks, method areas, local method stacks, and PC registers.

    • Heap: The heap is used to store object instances and array values, and it can be assumed that all the memory of objects created through new in Java is allocated, that the memory occupied by the objects in the heap is reclaimed by the GC, that the maximum is 2GB on a 32-bit operating system, and that there is no limit on 64-bit operating systems. The size can be controlled by-XMS and-xmx,-xms the minimum heap memory requested when the JVM starts, default to 1/64 of physical memory but less than 1GB;-XMX for the maximum heap memory that the JVM can request, default to 1/4 of physical memory but less than 1GB, By default, when the free heap memory is less than 40%, the JVM increases the size specified by the heap to-XMX, which can be specified by-xx:minheapfreeratio=, and when the free heap memory is greater than 70%, the JVM reduces the size of the heap to the size specified by-XMS. This ratio can be specified by-xx:maxheapfreeratio=, which is usually set to the same value as-XMS and-xmx, to avoid frequent resizing of the heap at run time for the operating system.
    • JVM method Stack: For thread-private, it is very efficient in memory allocation. When the method finishes running, the memory occupied by its corresponding stack frame is automatically freed. When the JVM method stack is running out of space, stackoverflowerror errors are thrown, which can be specified by-XSS in the Sun jdk.
    • Method extents: Information about the class to load (name, modifier, and so on), static variables in the class, constants defined in the class as final, field information in the class, and method information in the class. The method area is also shared globally, and under certain conditions it is also GC-outofmemory error message is thrown when the method area is using more memory than it allows. In the Sun JDK this area corresponds to the Permanet Generation, also known as the persistent generation, the default minimum value is 16MB, the maximum value is 64MB, the minimum and maximum values can be specified by-xx:permsize and-xx:maxpermsize.
    • Local method stack: Used to support the execution of the native method, which stores the state of each native method call. In the implementation of the Sun JDK, the JVM method stack is the same.
    • PC Register: The CPU register or operating system memory may be occupied.
2.Java Heap and Stack differences

Java divides memory into two types: one is stack memory, and the other is heap memory.

Some of the basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.

Heap memory is used to hold objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine. After an array or an object has been created in the heap, you can also define a special variable in the stack so that the value of the variable in the stack equals the first address of the array or object in the heap memory, and this particular variable in the stack becomes the reference variable of the array or object. You can then use the reference variable in the stack memory in your program to access the array or object in the heap, which is the equivalent of an alias, or codename, of an array or object.

A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses the new generation of arrays and objects, the heap memory that the array and the object itself occupies will not be freed, and arrays and objects become garbage, no longer used, but still occupy memory when no reference variable points to it. is released by the garbage collector at a later indeterminate time. This is also the main reason why the Java comparison occupies memory. But when writing a program, you can control it artificially.

3.Java memory leak and memory overflow

Memory leaks: Allocated memory is not recycled

Memory overflow: The system memory is not enough

4.Java class loading mechanism

The JVM divides the class loading process into three steps: Mount, link, and initialize.

    1. Load: The load process is responsible for locating the binary bytecode and loading into the JVM, which is passed the fully qualified name of the class (Com.bluedavy. HelloWorld) and Class loaders (Classloadera instances) to complete the loading of classes;
    2. Link: The link process is responsible for verifying the format of the binary bytecode, initializing the static variables in the load class and parsing the interfaces and classes called in the class;
    3. Initialization (Initialize): Performs initialization of static initialization code, constructor code, and static properties in a class.
5. Memory Recovery

Collector: Reference count collector, trace collector

    • Reference count Collectors: Reference count collectors are not ideal for languages where Java is an object-oriented type that can form complex referential relationships such as OBJECTB and OBJECTC, and the Sun JDK does not use this approach when implementing GC.
    • Trace Collector Implementation algorithm: Copy (Copying), Mark-Clear (Mark-sweep), and Tag-compress (Mark-compact)

Replication: When there are fewer surviving objects in the space to be reclaimed, the replication algorithm is more efficient and the cost is to increase an empty memory space and move the object.

Mark-Clear: more efficient in situations where there are many objects in space, but because tag-purge uses memory that is used to reclaim objects that do not survive directly, it can cause memory fragmentation.

Tag-Compression: It is also necessary to move objects on the basis of Mark-erase, which is relatively expensive and the benefit is not to generate memory fragmentation.

Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with

The mechanism of JVM memory management

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.