In-depth understanding of Java virtual Machines-garbage collection Overview

Source: Internet
Author: User

The first thing to clarify is that the garbage collection (GC) history is far older than Java, and when we realize the trouble of manually managing memory, lazy instincts drive pioneers to find simpler, easier-to-use, and key-to-fool memory management techniques. The emergence of GC technology from the Lisp language of MIT in the 1960 shows that the smarter the more lazy people are.

     Recently there is an idea: program development, programming is essentially a philosophical issue, although we have to solve the problem is the reality in the program world of projection, then the problem itself should be in the real world to have a corresponding solution, and we have been simulating the real world, by the program,         Object-oriented and functional programming, as well as the design of the GC we want to see below, are visible. Because we do not want to modify the code according to the different operating environment, we want a buffer layer to shield the underlying hardware environment, so we introduced the JVM    Because of the uncertainty of manually managing memory, JVM designers have introduced memory auto-allocation and garbage collection    Because you want to maximize memory, JVM memory is divided into thread memory (stack) and shared memory heap    In order to allocate and reclaim memory more efficiently, the heap is divided into the new generation and the old age, in order to adopt a more efficient GC scheme according to the different objects.    Also due to the requirements of different GC schemes, the Cenozoic is divided into Eden and survivor areas.    |----------Cenozoic------------|------old age------|    |          eden|survivor|survivor| Old |    ....    1. Program counter, virtual machine stack, local method stack three regions with this thread life and death, its memory allocation is confirmed, so do not have to think too much, we say that memory allocation and recycling is usually referred to as the heap    2. How do we tell if an object is "dead", that is, it can be recycled?    2.1 Reference counter method (not used)    The most intuitive method, if the object is referenced, is a reference count of +1, if a reference is referenced, then the reference count-1, if 0, the object is not referenced and can be recycled. However, the algorithm does not solve the problem of circular references. The Java Virtual machine also does not employ the algorithm.    2.2 Search Algorithms GC root tracing (Java, C #, Lisp)    Through a series of objects named "GC Root" as the starting point, searching down from that node, the path traversed is called the reference chain, and all objects that are not on any one of the reference chains (unreachable) can be judged to be unavailable.    The algorithm needs to determine the root point, including the following in Java: The object referenced by the local variable table in the virtual machine stack, the object referenced by the class static property in the method area, the object referenced in the method area, the object referenced in the local method stack Jni    2.3 Finalize    In a finalize block we can set the object to be re-referenced to avoid GC, but do not recommend any action in finalize fast. Because code blocks in finalize are placed in a queue and do not guarantee execution results.         3. References    Java references can be divided into four strong references (strong Reference), soft references (softreference), weak references (weakreference), virtual references (phantomreference), and references are strong to weak.    A strong reference is a common in code, similar to a reference to the object obj = new Obeject () class, which is never recycled    Soft references are used to describe something that is also useful, but not necessary, before an exception is thrown when a memory overflow occurs and a soft-referenced object is reclaimed two times and the memory is still not allocated.    A weak reference is used to describe a non-mandatory object, and the object that is associated with the weak reference intelligently survives until the next GC    A virtual reference is the weakest reference relationship that has no effect on the object, and its sole purpose is to receive a system notification when the object is reclaimed.    4. Recovery of method areas    The method area is also garbage collected, but the effect is usually not good. The garbage collection of the method area (permanent generation) consists mainly of two parts: obsolete constants and useless classes. In a large number of scenarios, such as reflection, dynamic Proxy, gclib and so on, the bytecode of virtual machines is required to ensure that the method area does not overflow.

In-depth understanding of Java virtual Machines-garbage collection Overview

Related Article

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.