Introduction to basic principles of memory management for Java virtual machines

Source: Internet
Author: User
Tags java se

Jdk:java programming language, Java Virtual machine, Java API Class library.

The JDK is the smallest environment for supporting Java program development.

Jre:java Java SE API subset in API class library, Java Virtual machine.

The JRE is a standard environment that supports the running of Java programs.

Program Counter Register: A small memory space that can be seen as the line number indicator of the bytecode executed by the current thread. is the only area in the Java Virtual Machine specification that does not specify OutOfMemoryError.

VM Stack: The same lifecycle and thread, which describes the memory model that Java methods perform: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on.

Two kinds of exceptions: Stackoverflowerror, OutOfMemoryError

Native Method Stack: Similar to VM stack, and Sun hotspot is directly merged!

Heap: The largest chunk of memory that is created when the virtual machine is started, with the sole purpose of storing the object instance.

MethodArea: Stores data such as class information, constants, static variables, and even compiler-compiled code that have been loaded by the virtual machine. The Java Virtual Machine specification is called the logical part of the heap, but the alias name is Non-heap (not a heap), or a permanent generation .

Runtime Constant Pool: Part of the method area

direcrtMemory: Is not part of the data area when the virtual machine is running, nor is it an area defined by the VM specification.

Object creation-keyword new

Memory Partitioning method : Pointer collisions (heap memory is normalized, memory is allocated by moving the pointer), idle list (heap memory is not structured, memory is allocated by updating memory list).

Tlab (thread local Allocation buffer): The local thread allocation buffer allocates a separate space for the created thread, avoids memory allocation collisions, and improves memory allocation efficiency.

the memory layout of the object : The object header (header), the instance data (Instance), and its padding (Padding).

Object Access : The Java program uses the reference (reference) data on the stack to manipulate specific objects on the heap.

Way: Handle and Direct pointer (Sun HotSpot).

OutOfMemoryError Exception Combat

Tools: Eclipse Memory Analyzer

Memory leak memories Leak, memory overflow memories Overflow

Determine if an object is dead- heap memory

1, reference counting algorithm: To add a reference counter to the object, whenever there is a place to refer to it is, the counter plus 1; When the reference fails, the counter minus 1, any time the counter is 0 of the object is impossible to be used again.

2, accessibility analysis algorithm: the "GC Roots" object as the starting point to search down, when an object to the GC Roots no reference chain connected to prove that this object is not available.

To is or not to be!

1, using the above algorithm to determine whether the object is used;

2, if no longer be used, then mark and filter;

Whether it is necessary to perform a finalize () method is not required to perform the following:

L object does not overwrite the Finalize () method;

The Finalize () method has been called by the virtual machine;

3, if it is necessary to execute, the object is placed in the F-queue queue;

4, by a virtual machine automatically established, low-priority finalizer thread to execute it, that is, by the virtual machine to trigger execution;

5, GC to the object in the queue will be a second mark, that is, to re-establish the reference to the object, you can remove the collection of collections;

Note: The Finalize () method of any object will only be called once by the system!

Method Area Recycling

Two main parts: Obsolete constants and useless classes

Control parameters

-XNOCLASSGC: Turn off the garbage collection function of the virtual machine to class;

-verbose:class: Monitor How many classes are loaded;

-xx:traceclassloading,-xx:traceclassunloading: Process information for the printing class to be loaded and unloaded;

Note: In a large number of scenarios, such as the use of reflection, dynamic proxies, cglib, bytecode frameworks, dynamically generated JSPs, and OSGi, such as frequent custom classloader, virtual machines are required to have the class offload function to ensure that the permanent generation does not overflow!

Garbage collection algorithm

1. Mark-Sweep algorithm: Easy to generate memory fragmentation

2. Replication algorithm: The heap is divided into two different areas: the New Generation (young) is mainly used to store the newborn objects, older generation (old) main storage applications in the long life cycle of memory objects. The New Generation (young) is divided into three regions: Eden, from Survivor, to Survivor.

By default, the new Generation (young) has a value of 1:2 for the Age (old)

Default, Edem:from:to = 8:1: 1

3. Marking-Sorting algorithm

4, Generational collection algorithm

The hotspot algorithm implements GC

Enumerate root nodes: check one by one; (Stop the WORLD,GC must pause all Java execution threads when executing)

With oopmap assistance, the hotspot can complete the enumeration quickly and accurately.

Safety point: Only at a safe point can a halt begin GC

Security Zone: The extension of the security point, where the security zone can be paused to start GC

Garbage collector: A specific implementation of memory reclamation.

GC Log

GC Common parameters

Memory allocation policy

1. Object priority in Eden allocation: Cenozoic GC (Minor GC), older generation GC (Major gc/full GC)

2, large objects directly into the old age:

3, long-term survival of the object will enter the old age: Define an Age counter for each object. The object is moved to survivor space and the object age is set to 1 after Eden is born and survives after the first minor GC and can be accommodated by Survicor. Objects in survivor each "through" a minor GC, the age increases by 1, when the age increases to a certain extent (the default 15), promoted to the old age.

4, the Dynamic object age judgment: The virtual machine is not always required object age to a certain extent to promote the old age, if the same age in Survivor space all object size sum greater than half of survivor space, the age is greater than or equal to the age of the object can be directly into the old era.

5, Space allocation guarantee: The old age of continuous space is greater than the total size of the new generation of objects or the average size of successive promotions will be minor GC, otherwise the full GC.

Tools

In JDK1.5, Getallstacktraces () is used to get stacktraceelement objects for all threads in the virtual machine.

Hsdis:jit generates code disassembly.

Jconsole:java Monitoring and management control platform, based on JMX.

VisualVM: Multi-in-a-fault processing tool

Introduction to basic principles of memory management for Java virtual machines

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.