Java Memory Recovery Mechanism (RPM)

Source: Internet
Author: User

Introduction

The Java heap is a run-time data area in which instances (objects) of the class allocate space. The Java Virtual machine (JVM) heap stores all objects created by running applications, which are established through directives such as new, NewArray, Anewarray, and Multianewarray, but they do not require program code to be explicitly released. In general, the heap is responsible for garbage collection, although the JVM specification does not require special garbage collection techniques or even garbage collection at all, but because of the limited memory, the JVM has a heap managed by garbage collection when implemented. Garbage collection is a dynamic storage management technology, which automatically frees objects that are no longer referenced by the program, and implements the function of automatic resource recovery according to the specific garbage collection algorithm.

The meaning of garbage collection

In C + +, the memory occupied by an object is occupied until the end of the program and cannot be assigned to other objects until it is explicitly released, whereas in Java the memory becomes garbage when there is no object reference to the memory originally assigned to an object. A system-level thread of the JVM automatically frees the block of memory. Garbage collection means that the object that the program no longer needs is "useless information," and that information is discarded. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is later used by the new object. In fact, in addition to releasing useless objects, garbage collection can also erase memory-logged fragments. The memory is fragmented because the object being created and the garbage collector frees up the memory space that the discarded objects occupy. Fragmentation is a free memory hole between the blocks of memory allocated to an object. Defragmentation moves the occupied heap memory to one end of the heap, and the JVM allocates the compiled memory to the new object.
Garbage collection can automatically free up memory space and reduce the burden of programming. This gives Java virtual machines some advantages. First, it can make programming more efficient. In the absence of a garbage collection mechanism, it may take a lot of time to solve a hard-to-understand memory problem. When programming in the Java language, the garbage collection mechanism can greatly shorten the time. Second, it protects the integrity of the program, and garbage collection is an important part of the Java language Security strategy.
One potential drawback of garbage collection is that its overhead affects program performance. The Java Virtual machine must trace the objects that are useful in the running program and eventually release the useless objects. This process takes the processor's time. Secondly, the incompleteness of garbage collection algorithm, some garbage collection algorithms used earlier can not guarantee that 100% collected all the discarded memory. Of course, with the continuous improvement of garbage collection algorithm and the efficiency of software and hardware running, these problems can be solved.

Several characteristics of garbage collection
    1. The unpredictability of garbage collection: Because different garbage collection algorithms are implemented and different collection mechanisms are used, it can happen periodically, possibly when the system is free of CPU resources, or it may be the same as the original garbage collection, which occurs when memory consumption is at its limit. This is related to the choice of the garbage collector and the specific settings.
    2. The accuracy of garbage collection: mainly includes 2 aspects: (a) The garbage collector can accurately mark the Living object, (b) The garbage collector can accurately locate the reference relationship between objects. The former is a precondition for completely reclaiming all discarded objects, or it may cause a memory leak. The latter is necessary for the realization of algorithms such as merging and copying. All unreachable objects can be reliably recycled, all objects can be reassigned, objects are copied and object memory is shrunk, which effectively prevents fragmentation of memory. (c) There are many different kinds of garbage collectors, each with its algorithm and its performance, both stopping the application when garbage collection starts, and allowing the application's thread to run when garbage collection starts, as well as the garbage collection multithreading at the same time.
    3. The implementation of garbage collection is closely related to the specific JVM and the JVM's memory model. Different JVMs may have different garbage collections, and the JVM's memory model determines what types of garbage collection the JVM can take. The memory systems in the HotSpot family of JVMs are now designed with an advanced object-oriented framework, allowing the JVM to use state-of-the-art garbage collection.
    4. With the development of technology, modern garbage collection technology offers many optional garbage collectors, and different parameters can be set when each collector is configured, which makes it possible to obtain optimal application performance according to different environments.

In view of the above characteristics, we should pay attention to when using:

    • Do not attempt to assume that garbage collection takes place at any time, all of which are unknown. For example, a temporary object in a method becomes a useless object when the method call is complete, and its memory can be freed at this time.
    • Java provides a number of classes that deal with garbage collection and provides a way to enforce garbage collection-calling System.GC (), but this is also an indeterminate approach. Java does not guarantee that each call to this method will be able to start garbage collection, it will only issue to the JVM such a request, whether or not to actually perform garbage collection, everything is unknown.
    • Pick the right garbage collector for you. In general, if your system does not have special and demanding performance requirements, you can use the JVM's default options. Otherwise, you might consider using a targeted garbage collector, such as an incremental collector, which is more suitable for systems with higher real-time requirements. The system has a high configuration, there are more idle resources, you can consider using the parallel tag/purge collector.
    • The key is also difficult to grasp the problem is the memory leak. Good programming habits and rigorous programming attitude is always the most important, do not let your own a small error caused a large memory leak.
    • Release references to useless objects as early as possible. Most programmers use temporary variables when the reference variable is automatically set to null after exiting the active domain (scope), implies that the garbage collector collects the object, and must be aware that the referenced object is listening, and if so, remove the listener and then assign a null value.
Conclusion

In general, Java developers can not focus on the allocation of heap memory and garbage collection in the JVM, but fully understanding the Java feature allows us to use resources more efficiently. Also note that the Finalize () method is the default mechanism for Java, and sometimes you can write your own Finalize method to ensure explicit release of object resources.

Java Memory Recovery Mechanism (RPM)

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.