Garbage collector GC

Source: Internet
Author: User

Garbage collection mechanism in Java

An important feature of Java is the garbage collector, and the ability to automatically recycle garbage, which is the advantage of Java relative to other languages, such as the C language there is no automatic garbage collection mechanism.

Every instance object and array in Java allocates space within the heap, the interpreter has the ability to allocate space for the class instance, and when the interpreter allocates storage space for an instance, it records the use of the memory area occupied by the instance object, and once the object is used, it is recycled into garbage!

In the Java language, there is no other way out of the new statement to request and free memory for an object, and the work of freeing and reclaiming memory is assumed by the Java Runtime System. In the Java interpreter and hot Java environments developed by Sun, the way in which background threads are recycled after a fragment is recycled not only provides good performance to the system but also allows our programmers to get rid of their own risk of controlling memory usage. The automatic garbage collection feature in Java addresses the two most common application errors: Memory exposure and invalid memory references.

The importance of cleaning up garbage data is often overlooked by programmers, which can be dangerous, causing garbage to occupy system resources until memory is exhausted. Java provides a mechanism called garbage collection to prevent programmers from ignoring garbage processing, and Java automatically helps us with garbage collection without the programmer having to think about it.

When the Java program is running, a garbage collection machine will be awakened periodically to check for any objects that are no longer being used, to reclaim and then release the resources (space), but the garbage collection mechanism is not controlled by our programmers, and it is possible that the garbage collector is not started when a program finishes running. So this is not a very reliable mechanism, but this is not a bad thing, because the garbage collection mechanism will bring additional burdens to the system resources. The smaller the chance of starting up, the less the additional burden is imposed.

Different Java virtual machines use different recycling strategies: 1. Replication 2. Self-reflection

1. Replication: is to suspend the running program, copy all the objects being used to another heap memory, the unused memory will be released.

This type of replication requires twice times the amount of memory required to maintain, even if your garbage is small, the contents of the heap memory will be copied into another heap of memory, which makes this strategy inefficient.


2. Self-reflection:

The introspection collector detects all objects that are being used and then marks them, freeing the space occupied by all objects that are no longer in use once the task is completed. But this is a very slow way, but if the program's garbage is very small, such a strategy is very advantageous.


So the two methods can be used together, in some JVMs in the combination of two ways, in practical applications, the JVM will supervise the operation of the two modes of efficiency, if the object in the program is used for a long time, the JVM will be converted to "introspection mode" recycling mode, When a large amount of garbage or the object is more occupied by the memory of the situation is serious, it will be converted to "replicated" mode, so loop. Implement two mechanism exchanges.



This article from "Love Ji" blog, declined reprint!

Garbage collector GC

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.