Java Virtual machine garbage collector and memory allocation policy

Source: Internet
Author: User
Tags reflection

  What memory needs to be recycled?

Java Memory Runtime area of the various parts, where the program counter, virtual machine stack, the local method stack 3 regions with the thread, with the thread, the stack frame in the stack with the method entered and exited with a non-flocculation of the execution of the stack and into the stack operation. How much memory is allocated in each stack frame is basically known when the class structure is determined. Therefore, memory allocation and recycling are deterministic in these areas, so this part does not require much consideration for memory reclamation. But the method area is different from the heap, the memory of multiple implementations in an interface may not be the same, the memory required for multiple branches in a method is different, we only know what objects are created when the program is running, and this part of the allocation and recycling is dynamic, and the garbage collector is concerned about this part of memory.

Object PAIRS by:

References: After jdk1.2, references are divided into strong references, soft references, weak references, and virtual references:

Strong references: Universally present in programs, such as Object Obj=new object (); This type of reference, as long as a strong reference is still in, the garbage collector will never reclaim the referenced object.

Soft references: Used to describe some useful but not necessary objects. These objects will be listed in the Reclaim scope for a second collection before the system will have a memory overflow exception. A SoftReference class is provided to implement a soft reference.

Weak reference: Used to describe a non-required object, but its strength is weaker than soft reference, and the object associated with the weak reference only survives until the next garbage collection occurs. When the garbage collector is working, the objects associated with a weak reference are reclaimed regardless of whether the current memory is sufficient.

Virtual reference: The weakest reference relationship, whether an object has a virtual reference, does not affect its lifetime at all, and cannot be taken from an object instance by a virtual reference. The sole purpose of setting a virtual reference association for an object is to be notified by a system when the object is reclaimed by the collector. A phantomreference class is provided to implement a virtual reference.

  

  Recovery method Area:

Many people think that the method area (hotspot called permanent generation) does not require garbage collection, and the Java Virtual Machine specification does say that virtual machines can not be required to implement garbage collection in the method area, and that the cost/performance of garbage collection in the method area is generally relatively low.

Garbage collection mainly recycles two parts: obsolete constants and useless classes.

Recycling obsolete constants is similar to retrieving objects in the Java heap, such as "ABC" has entered a constant pool, but there is no string object called "ABC" in the system, and there is no place to reference the "ABC" in the constant pool, and this "ABC" will be cleaned up. Symbolic references to other classes (interfaces), methods, and fields in a constant pool are similar.

Recycling Useless classes: classes need to meet the following 3 conditions in order to be useless classes (useless classes can be recycled) (in a large number of reflection, dynamic Proxy, dynamic generation of JSP, frequently defined ClassLoader scenarios require the virtual machine has the class unloading function to ensure that the permanent generation does not overflow):

1. All instances of this class have been recycled

2. The classloader that loaded the class have been recycled

3. Loading the corresponding Java.lang.Class object is not referenced anywhere, and it is not possible to access the method of the class from anywhere by reflection.

Garbage collection algorithm:

1. Mark-Sweep algorithm

You first need to tag all objects that need to be recycled, and all of the tagged objects are collected uniformly after the tag is complete.  The tagging process generally uses the accessibility analysis algorithm rather than the reference counting algorithm. It mainly has two points insufficient: 1 is the efficiency problem, the marking and clears two processes the efficiency is not high, another is the space problem, after the mark clears produces the massive discontinuous memory fragmentation, the memory fragmentation too many may cause later when the program runs the need to allocate

    

    

2, replication algorithm (to solve the problem of efficiency)

The replication algorithm divides the available memory by capacity into two blocks of equal size, using only one block at a time, and when this piece of memory is exhausted, copies the surviving objects to the other, then cleans up the used memory space once. This makes it possible to reclaim memory for the entire half of the time, and memory allocations do not have to account for complex situations such as memory fragmentation.

IBM Research, the new generation of object 98% is to die, do not need 1:1 partition memory space, but the memory is divided into a large Eden space and two smaller survivor space. Use Eden and one piece of survivor at a time. When recycled, objects that are still alive in Eden and survivor are copied to another survivor space at once, and finally the Eden and the used survivor space are cleared. While the size ratio of the hotspot virtual machine default Eden and Survivor is 8:1:1, we have no way to guarantee that only a few 10% objects per collection will survive, and when the survivor space is not enough, you need to rely on other memory for allocation guarantees.

    

      

Java Virtual machine garbage collector and memory allocation policy

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.