Chapter III JVM Memory Reclaim area + object survival judgment + reference type + garbage collection thread

Source: Internet
Author: User

Note: This article mainly refers to the in-depth understanding of Java Virtual Machine (second edition)

Description: Before viewing this article, it is recommended to know the JVM memory structure, see chapter I JVM Memory structure

1. Area of memory recovery

    • Heap: This is the main area of the GC
    • Method Area: Recycle two things
      • Useless class
      • Obsolete constants
    • Stack and PC registers are thread-private areas and do not occur GC

2. How to judge whether an object survives

Garbage collection: Reclaims the memory that the dead object occupies. There are two ways to determine if an object is dead:

    • Reference counting method
      • Principle: Add a reference counter to the object, and whenever there is a place to reference it, the counter value +1; When the reference fails, the counter value-1
      • Two reasons not to use it in practice
        • Each time the object is assigned a value, the counter value of the increase or decrease, consumption of large
        • For a, B mutual reference this situation can not handle (this is not the main reason)
    • Accessibility analysis (Trace collection)
      • Principle: Starting from the root collection (GC Roots) to scan down, the root set of nodes can reach the node is the surviving node, the root set of nodes will not reach the node is to be recycled dead node, such as the a/b/c is the survival node, d/e is the death node:

      • the nodes in the root collection include : Simply speaking, global references ( constants and static properties ) and stack references (first to third below)
        • object references in the Java stack (present in the local variable table, note: The local variable table holds the base data type and object references )
          • This is where garbage collection is most considered, so sometimes we also call the Dead object "No reference point to the object"
        • Method Area: Constant + static (statics) variable
        • To a local method, an object reference that has not been disposed by the local method

3, 3 types of references

    • Strong reference (Strong Reference): A A = new A ();//a is a strong reference
    • Soft reference (Soft Reference): Releases the object referenced by a soft reference when memory is low, or a normal object when memory is sufficient (strong reference)
    • Weak references (Weak Reference): Weak reference objects can only survive until the next garbage collection, and are immediately reclaimed once garbage collection occurs

4, the method area recovery

    • Obsolete constants: For example, if there is no reference to the "ABC" string in the constant pool, the "ABC" string is recycled
    • Useless class: Meet the following three conditions
      • No instances of the class exist in the Java heap
      • The ClassLoader that loaded the class are recycled
      • The class object of this category is not referenced anywhere

Attention:

    • In the actual development, as far as possible without JSP to do the front end, but with velocity, freemarker such a template engine to do
    • Three parameters commonly used in relation to a class:
      • -xx:+printclasshistogram: Output class statistics status
      • -xx:-traceclassloading: Print class Load Information
      • -xx:-traceclassunloading: Print class unload information View Code

5. Garbage collection Thread

A system's garbage collection is a garbage collection thread that detects an operation that is a background thread (daemon thread).

5.1, background thread and the foreground thread we use, there is a feature: when the number of foreground threads in the JVM is 0 o'clock, the background thread dies automatically . It can be said that the background thread relies on the foreground thread and exists.

5.2. Why should the garbage collection thread be set as a background thread?

Let's think about it, when there is no one thread in the current station, will there be garbage? Or is it necessary to recycle garbage? The answer is no, so this time the garbage collection thread loses the meaning of survival.

So it can be said that if a thread is set as a background thread, it will be possible to see if the thread still has the meaning of survival without other threads in existence .

For example, when we use Apache Mina2 to do RPC, we open a background thread at the receiving end of the message to start the service to accept message event requests from the sending end. Try to think, if there is only the current background thread in the entire JVM, is it necessary for this thread to survive? Of course there is no need, because the message will never be sent again (foreground threads are gone)

Chapter III JVM Memory Reclaim area + object survival judgment + reference type + garbage collection thread

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.