GC (Garbage collection)

Source: Internet
Author: User

The memory allocation and recycling of Java programs is performed automatically by the JRE in the background. The JRE is responsible for reclaiming memory that is no longer used, a mechanism known as garbage collection GC. Typically, the JRE provides a super thread for detection and control, typically by automatically GC when the CPU is idle or out of memory, and the programmer cannot precisely control the GC's time and order.

Java's heap memory is a run-time data area that holds instances (objects) of a class, and the Java Virtual machine's heap memory stores all objects that are created by running applications that do not require the program to be explicitly freed by code. In general, heap memory is the responsibility of the GC, and all JVMs have a heap memory managed by the GC mechanism in the implementation. GC is a dynamic storage management technology that automatically frees objects that are not referenced by the program and implements automatic recovery of memory resources according to specific GC algorithms.

When no variable points to the memory that was originally assigned to an object, the memory becomes garbage. A super thread of the JVM automatically frees the memory area. GC means that the object that the program no longer needs is "spam", and that information is discarded.

When an object is no longer referenced, the memory reclaims the space it occupies so that space can be used by new objects in the background. In fact, in addition to releasing useless objects, the GC can also erase memory-logged fragments. The memory is fragmented because the creation object and the garbage collector free up the space occupied by discarded objects. Fragmentation is an area of free memory that is allocated to an object's memory block, and defragmentation moves the occupied heap memory to one end of the heap, and the JVM allocates the compiled memory to the new object.

GC can automatically free up memory space, reduce the burden of programming. This enables Java virtual machines to have two significant benefits

    • Improve programming efficiency
    • GC is an important part of the Java language Security strategy.

A potential disadvantage of GC

    • Its overhead affects the performance of the program.

      The Java virtual machine must keep track of the objects that are useful in the program before you can determine which objects are useless objects and eventually dispose of those useless objects. This process takes the processor's time. The second is the incompleteness of the garbage collection algorithm. Of course, with the continuous improvement of garbage collection algorithm and the increasing efficiency of hardware and software, these problems can be solved.

Usually the GC consists of several features as follows:

    • The purpose of GC is to reclaim the memory space of useless objects, which are memory space in the JVM heap memory, the GC can only reclaim memory resources, and do nothing to other physical resources, such as database connection, disk IO and other resources.
    • In order to allow the GC mechanism to reclaim objects that are no longer in use, it is possible to imply that the GC mechanism can reclaim the object by setting its reference variable to NULL.
    • Unpredictable. Different JVMs use different garbage collection mechanisms and different garbage collection algorithms.

The accuracy mainly includes 2 aspects.

    • Precise labeling of Living objects
    • Precisely locates the reference relationship between objects.

When we write Java programs, the basic principle is: do not reference them again for objects that are no longer in use. If we keep a reference to these objects, the GC mechanism will not reclaim the object for the time being, resulting in less available memory for the system. The higher the frequency of GC execution, the lower the performance of the system.

GC (Garbage collection)

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.