Deep understanding of Java Virtual Machine---garbage collector and allocation policy-1

Source: Internet
Author: User
Tags joins

Blog Highlights:

Learning goals: which memory needs to be recycled

When is it recycled?

How to Recycle

In the model based on the concept discussion, the Java heap and the method area are discussed mainly.

Why?: multiple implementation classes in an interface may require different memory, and multiple branches in one method may require different memory. Only when the program is running can you know what objects will be created, and this part of the memory allocation and recycling is dynamic, the GC is concerned about this piece of memory.

Which memory needs to be recycled:

Determine if the object is alive:

Reference counting algorithm: A reference counter is added to the object, there is a place to reference it and the counter is incremented by 1, minus 1 when the reference fails. Objects that are referred to as 0 are not available.

Advantages: Simple to achieve, high efficiency of judgement.

Cons: Cannot resolve circular references between objects, see Code.

1  Public classREFERENCECOUNTINGGC {2      PublicObject instance =NULL;3 4     Private Static Final int_1MB = 1024 * 1024;5 6     Private byte[] Bigsize =New byte[2 *_1MB];7 8      Public Static voidTESTGC () {9REFERENCECOUNTINGGC Obja =NewREFERENCECOUNTINGGC ();TenREFERENCECOUNTINGGC OBJB =NewREFERENCECOUNTINGGC (); OneObja.instance =OBJB; AObjb.instance =Obja; -          -            theObja =NULL; -OBJB =NULL; -          -         //Although the reference count is 1, the memory is still recycled, indicating that the reference counting algorithm is not used + System.GC (); -     } +  A      Public Static voidMain (string[] args) { at TESTGC (); -     } -}
View Code

Accessibility analysis algorithm: The idea, select a series of objects called "GC Roots" as the starting point, from these nodes to search down, the path traversed is called the reference chain. If an object cannot reach "GC roots" through a reference chain, it proves that the object is not available and can be recycled.

Objects that can be used as GC roots: objects referenced in the virtual machine stack, objects referenced by the method zone class static properties, objects referenced in the method area constants, objects referenced in the Nativa method. TODO: Understanding GC Roots

Reference:

TODO: various application scenarios

Reference refinement Definition: When the memory space is sufficient, it can remain in memory. If the memory space is still very tense after garbage collection, discard the objects.

Based on this requirement, the concept of reference is expanded.

Strong references: As long as a strong reference exists, it will never be GC. eg. Object obj = new Object ();

Soft references: When memory is sufficient, it is not recycled and is recycled when it is insufficient. The JVM joins this soft reference to the reference queue associated with it

Weak references: They are recycled regardless of memory adequacy. The JVM joins this weak reference to the reference queue associated with it

Virtual References:

    

Two tokens for objects: If the object does not have a GC roots to the reference chain after the first accessibility analysis, the first token is made. and enter the first self-help process, if the object overrides the Finalize () method && the Finalize () method is not called by the virtual machine, then the Finalize () method is executed to save the process, put the object into a f-queue to the queue, The low-priority finalize thread is automatically created by the virtual machine to execute (but not guaranteed to wait for the method to run to the end for efficiency reasons). If a reference to the object is assigned to a class variable or member variable in the Finalize () method, and the reachable relationship is re-established, the second marking process will be moved out of the collection of "going to be recycled", but be aware that such self-help can only be performed once.

    

1  Public classFINALIZEESCAPEGC {2      Public StaticFINALIZEESCAPEGC Save_hook =NULL;3      Public voidisAlive () {4System.out.println ("Yes, I am still alive");5     }6 7 @Override8     protected voidFinalize ()throwsThrowable {9System.out.println ("Finalize Method excute!");TenFinalizeescapegc.save_hook = This; One     } A  -      Public Static voidMain (string[] args)throwsinterruptedexception { -Save_hook =NewFINALIZEESCAPEGC (); the  -         //The first time to save your own success -Save_hook =NULL; - System.GC (); +  -Thread.Sleep (500); +         if(Save_hook! =NULL) { A save_hook.isalive (); at}Else { -System.out.println ("Dead"); -         } -  -         //the second time to save yourself failure, can only be executed once -Save_hook =NULL; in System.GC (); -  toThread.Sleep (500); +         if(Save_hook! =NULL) { - save_hook.isalive (); the}Else { *System.out.println ("Dead"); $         }Panax Notoginseng     } -}
View Code

Reclamation of method zones (permanent generations): recycling of obsolete constants and useless classes mainly.

Obsolete constants: Eg: "abc" exists in a constant pool, but there is no other reference to the constant, class, method, and the symbolic reference to the field is similar.

Useless class: All instances of this class have been recycled

The ClassLoader that loaded the class has been recycled

The corresponding class object is not referenced and cannot be used to access the class's methods through reflection elsewhere.

The classes that satisfy these conditions can be recycled and recycled, depending on our parameter settings for the virtual machine.

Usage scenarios: In a large number of scenarios where the use of reflection, dynamic proxies, cglib and other frequently defined self-classloader requires a virtual machine with class offload capabilities

In-depth understanding of Java Virtual Machine---garbage collector and allocation policy -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.