Maximum use of JVM heap memory based on Java soft-reference mechanism and eliminate outofmemory

Source: Internet
Author: User

Preface: A good one week two articles in countless trivia and their laziness did not do well, in this expression of their dissatisfaction and the strict implementation of the people have a deep respect!!!!

--------------------------------------------------------------------------------------------------------------- ------------------

Citation: Java programmer is not unfamiliar with outofmemory, in general, this exception occurs mainly because the application cache a large amount of data is not caused by GC out of the heap memory overflow, but many times, in order to reduce duplication or increase the speed of running, you need to cache some data, For example, loading configuration file information, initializing the information from the database, and some intermediate results obtained during the operation. Programmers in the JVM to load the data is often very tangled, on the one hand want to cache the more the better, to minimize the library and repeat the calculation, but on the other hand too much cache to the GC pressure, and even to worry about overflow problem.

Requirements: If there is a way to cache the data as much as possible to improve operational efficiency, but also can actively empty a portion of expired data before the GC to prevent memory overflow, how good. Below, I want to talk about the implementation of heap memory monitoring based on Java soft Reference, which is the author's practice in the production system, may help the programmer to do some experiments in this aspect.

Introduction: The article will first explain what is a soft reference, followed by the GC of soft reference processing characteristics, around its characteristics using the JDK's own related classes to elaborate code implementation details.

Body:

1. What is a soft reference:

We know that there are four referential relationships in Java, namely strong, soft, weak, and virtual, such as:

Strong reference: the JVM memory manager departs from the root reference collection (root set) for all reachable object reference relationships in the heap, and is also a common reference type, such as Object obj = new Object (), which must not be recycled as long as a strong reference exists.

Soft references: used to describe some useful but not necessary objects, represented in Java by the Java.lang.ref.SoftReference class. If an object has only soft references, enough memory space is available, the garbage collector does not recycle it, and if the memory space is insufficient, the memory of those objects is reclaimed.

Weak references: used to describe non-essential objects, in Java, represented by the Java.lang.ref.WeakReference class. When the JVM is garbage collected, the object associated with the weak reference is reclaimed, regardless of sufficient memory.

Virtual References: can be applied at any time by objects recycled by the garbage collector, represented by the Java.lang.ref.PhantomReference class. If an object is associated with a virtual reference, it is associated with no reference, and the referential relationship is more associated with the virtual reference queue to facilitate some GC monitoring.

2. Soft reference Features

According to the Java Help document: " a soft reference object is determined by the garbage collector to clear this object when it responds to memory needs." Soft reference objects are most commonly used to implement memory-sensitive caches. Assume that the garbage collector determines that an object is a soft reachable object at a point in time. At this point, it can choose to automatically clear all soft references to the object, as well as all soft references to any other soft reachable object through a strong reference chain, from which it can reach the object. At the same time or later, it joins the queue with the newly purged soft references that have been registered with the reference queue. all soft references to soft reachable objects are guaranteed to be cleared before the virtual machine throws OutOfMemoryError. Otherwise, the time to clear a soft reference or to clear a set of such references to different objects will not be constrained in any order. However, virtual machine implementations do not encourage the removal of soft references that have been recently accessed or used. A direct instance of this class can be used to implement a simple cache, and the class or its derived subclasses can also be used for larger data structures for more complex caches. Soft references are not purged as long as the soft reference indicates that the object is a strongly reachable object, that is, the object that is being actually used. For example, complex caches can prevent the most recently used items from being discarded by keeping the strongly directed object of the most recently used item and by the garbage collector deciding whether to discard the remaining items. "

As we know from the above, the soft reference object is guaranteed to be recycled by the JVM before OutOfMemoryError, and it is added to its registered purge queue. Therefore, by monitoring whether the queue has a soft reference object that is about to be purged, we can indirectly know whether the Java application has reached the edge of the overflow crash, and quickly perform some of the cache data emptying before it overflows so that the virtual machine can clean up some memory to avoid the overflow of heap memory, and further, We can set the soft reference object to a certain amount of memory size of the object, such as 10M, so when the virtual machine memory is low, the first time the object will be reclaimed to free up 10M of free memory, thereby alleviating the memory shortage, and for the application to obtain a valuable empty part of the cache data time, effectively avoid directly throwing memory overflow exception.

3. Implementation Details

Based on the above analysis and practical development practices, the code for monitoring virtual machine memory usage using soft reference objects is implemented as follows:

  1. Initializes a soft reference object to the reference queue and sets the soft reference object to occupy 10M of memory
    1 //Memory Monitoring2      Public Staticreferencequeue<byte[]>Memorydetectorqueue; 3      Public Staticsoftreference<byte[]>Memorydetector;4     5     //Initial6      Public Static voidInitial () {7Memorydetectorqueue =Newreferencequeue<byte[]>();8Memorydetector =Newsoftreference<byte[]> (New byte[(int) (10*1024*1024)],memorydetectorqueue);9}

  2. Set up a separate thread, start the thread after the soft-reference object is initialized, start monitoring whether the memorydetectorqueue is non-empty, and non-empty indicates that the soft-reference object is out of memory because of insufficient space to clean up the memory:
    1  Public classMemorymonitorserviceImplementsRunnable {2 3      Public voidrun () {4          while(true) {5             Try {6                 if(Memorydetectorqueue.remove ()! =NULL) {7Dopartclean ();//performs partial cache emptying to free memory, and can perform cleanup based on some LRU algorithms or proportionally8                 }9}Catch(Exception e) {TenLogger.error ("", e); One}finally{ AMemorydetector =Newsoftreference<byte[]> (New byte[(int) (10 * 1024 * 1024)], -Memorydetectorqueue);//re-create soft reference objects after performing partial cache cleanup -             } the         } -     } -}

    Description: The Memorydetectorqueue.remove () method waits until it is blocked until an object becomes available, and the value returned is not NULL when the Memorydetector soft Reference object has been GC dropped.

  3. Call New Memorymonitorservice (). Start () starts the monitoring thread. In general, the Dopartclean () work in the above code is assisted by a specialized cleanup class.

Maximum use of JVM heap memory based on Java soft-reference mechanism and eliminate outofmemory

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.