Java garbage collection and memory allocation policy

Source: Internet
Author: User

What is garbage collection?

Garbage Collection GC (Garbage Collection) is one of the core technologies of the Java language, and the purpose of garbage collection is to purge objects that are no longer in use. The GC determines whether to collect the object by determining whether the object is referenced by the active object. The GC first determines whether the object is ready to be collected. Two common methods are reference counts and object reference traversal.

How do I tell if an object needs to be collected?

reference count (the simplest and oldest method) : Saves the number of references to a resource (which can be objects, memory, or disk space, and so on) and releases it when the number of references becomes 0 o'clock.

Reference count Flaw: reference count does not solve circular reference problem: Suppose that the object A, a, is already instantiated, let a=b,b=a, otherwise these two objects no longer have any reference, at this time the value of the counter can never be 0, but the reference counter cannot notify the GC to reclaim them.

Object reference Traversal (now used by most JVMs) : Object reference traversal starts with a set of objects, recursively determines the reachable (reachable) object, along each link on the entire object graph. If an object cannot arrive from one (at least one) of these root objects, it is garbage collected.

Reachable: means that it can be accessed through a reference, such as: a =new a (); Create a space in the stack to store this object A, through which a object can access the new space, then it is reachable. If it is just new A (); That can only be accessed in this sentence, such as New A (). someThing (); There is no way to access new space anywhere else. In the case of a linked list, the deletion of a node is to point to the node's reference to another node, so that the deleted node is equivalent to the outside world lost contact, it can not be accessed, and then the recycling mechanism to reclaim this inaccessible object occupied by the stack memory.

The difference between four types of references in Java

Strong reference: If an object has a strong reference, it will not be reclaimed by the garbage collector. Even if there is not enough memory space, the JVM does not reclaim it, but instead throws a outofmemoryerror error that causes the program to terminate unexpectedly. If you want to break the association between a strong reference and an object, you can explicitly assign a reference to NULL, so that the JVM reclaims the object at the appropriate time.
Soft references: When using soft references, if there is enough memory space, soft references can continue to be used without being reclaimed by the garbage collector, and soft references are reclaimed by the garbage collector only when there is insufficient memory.
Weak reference: An object with a weak reference has a more ephemeral life cycle. Because when the JVM is garbage collected, weak references are recycled whenever a weak reference object is found, regardless of whether the current memory space is sufficient. However, because the garbage collector is a low-priority thread, it is not always possible to quickly discover weak reference objects
Virtual reference: As the name implies, is the form of a dummy, if an object only holds a virtual reference, then it is equivalent to no reference, at any time may be reclaimed by the garbage collector.

See more: http://www.cnblogs.com/alias-blog/p/5793108.html

Methods of garbage collection

Tag recycling: Iterate through the object graph and record the reachable objects to remove unreachable objects, typically using single-threaded work and potentially generating memory fragmentation.
Tag-Compression recovery method: Earlier and the first method is the same, just one more step, all the surviving objects are compressed to one end of the memory, so that the memory fragment can be synthesized a large number of reusable memory area, improve memory utilization.
Copy recovery method: divides the existing memory space into two parts, when the GC runs, it copies the reachable object to the other half of the space, and then empties the entire object of the space in use. This method is suitable for short-lived objects, and continuous replication of long-lived objects results in reduced efficiency.
Generational recycling: The memory space is divided into two or more domains, such as the young generation and the old age, the young generation is characterized by the object will be quickly recycled, so in the younger generation use more efficient algorithms. When an object is still alive after several recoveries, the object is put into memory space called old age, and the old age takes the tag-compression algorithm.

Java garbage collection 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.