JAVA--GC recovery mechanism

Source: Internet
Author: User

1. What is a GC?

GC (Gabage Collection): The fundamental purpose of Java's garbage collection mechanism is to keep track of objects that are in use, and then reclaim objects that are no longer used (referenced). The GC recovery mechanism for Java has two benefits:

(1) Avoid memory exhaustion caused by excessive garbage

(2) Avoid illegal memory references due to improper memory release

The GC primarily identifies the heap objects in the JVM, and if the object is being referenced, it is a live object. Otherwise it is dead and can be recycled and redistributed.

2. How is the memory of Java stored?

Heap: Used to store objects created in Java, new objects exist in the Java heap

Stacks: Used to store references to objects in Java, common variables, static variables, final variables, and so on, except for objects that are basically stored here.

3. What are the conditions that the GC triggers? Is it possible to manipulate it artificially?

There are two types of GC in the JVM, one is the primary GC, the other is the secondary GC, and the primary GC has the lowest priority, so it is rarely triggered. The frequency at which the thread of the GC is triggered is relatively high.

Two conditions triggered by the main GC

(1) The thread priority of the main GC is very low, so it is called only when the program is idle, except for the 2nd condition

(2) The GC is called when the Java heap memory is insufficient. And if the GC still fails to meet the memory allocation after a single time, the JVM will make another two GC for further attempts, and if the new object cannot be created, the JVM will report an out of memory error and the Java program will launch.

However, since GC is Java's own garbage collection mechanism, it is considered that writing code cannot be handled, but it can be thought that calling System.GC (), a reminder call, still does not affect the object's recycling order and the payback time. In addition, the main GC in Java is determined by the JVM based on the environment, so it is not possible to run the GC properly. But it must have been repeated.

4. How can I reduce the cost of GC? The basic principle is to minimize waste

(1) Reduce human intervention, that is, manually call System.GC (). Although the above mentioned manual intervention is only a recommendation, it does trigger the main GC, increasing the number of pauses

(2) Use static global variables defined by static as little as possible, and static global variables will not be recycled by GC, but always occupy memory

(3) Use more basic data types such as int,long,float, less create the corresponding integer and other objects, reduce memory consumption. As much as using StringBuffer, use less string, because string will create many string objects as it operates.

(4) Avoid sudden creation of large numbers of objects or sudden deletion of large numbers of objects, both of which may trigger GC

(5) Reduce the use of temporary objects, reduce garbage, or after the object is used to complete, the display is set to NULL, convenient GC for direct recycling

JAVA--GC recovery mechanism

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.