How to force garbage collection in Java?

后端 未结 22 1406
离开以前
离开以前 2020-11-22 00:31

Is it possible to force garbage collection in Java, even if it is tricky to do? I know about System.gc(); and Runtime.gc(); but they only suggest t

相关标签:
22条回答
  • 2020-11-22 00:48

    To manually Request GC (not from System.gc()) :

    1. Go To : bin folder in JDK eg.-C:\Program Files\Java\jdk1.6.0_31\bin
    2. Open jconsole.exe
    3. Connect to the desired local Process.
    4. Go To memory tab and click perform GC.
    0 讨论(0)
  • 2020-11-22 00:48

    If you are using JUnit and Spring, try adding this in every test class:

    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
    
    0 讨论(0)
  • 2020-11-22 00:51

    The jlibs library has a good utility class for garbage collection. You can force garbage collection using a nifty little trick with WeakReference objects.

    RuntimeUtil.gc() from the jlibs:

       /**
        * This method guarantees that garbage collection is
        * done unlike <code>{@link System#gc()}</code>
        */
       public static void gc() {
         Object obj = new Object();
         WeakReference ref = new WeakReference<Object>(obj);
         obj = null;
         while(ref.get() != null) {
           System.gc();
         }
       }
    
    0 讨论(0)
  • 2020-11-22 00:51

    The best (if not only) way to force a GC would be to write a custom JVM. I believe the Garbage collectors are pluggable so you could probably just pick one of the available implementations and tweak it.

    Note: This is NOT an easy answer.

    0 讨论(0)
  • 2020-11-22 00:51

    Another options is to not create new objects.

    Object pooling is away to reduce the need GC in Java.

    Object pooling is generally not going to be faster than Object creation (esp for lightweight objects) but it is faster than Garbage Collection. If you created 10,000 objects and each object was 16 bytes. That's 160,000 bytes GC has to reclaim. On the other hand, if you don't need all 10,000 at the same time, you can create a pool to recycle/reuse the objects which eliminates the need to construct new objects and eliminates the need to GC old objects.

    Something like this (untested). And if you want it to be thread safe you can swap out the LinkedList for a ConcurrentLinkedQueue.

    public abstract class Pool<T> {
        private int mApproximateSize;
        private LinkedList<T> mPool = new LinkedList<>();
    
        public Pool(int approximateSize) {
            mApproximateSize = approximateSize;
        }
    
        public T attain() {
            T item = mPool.poll();
            if (item == null) {
                item = newInstance();
            }
            return item;
        }
    
        public void release(T item) {
            int approxSize = mPool.size(); // not guaranteed accurate
            if (approxSize < mApproximateSize) {
                recycle(item);
                mPool.add(item);
            } else if (approxSize > mApproximateSize) {
                decommission(mPool.poll());
            }
        }
    
        public abstract T newInstance();
    
        public abstract void recycle(T item);
    
        public void decommission(T item) { }
    
    }
    
    0 讨论(0)
  • 2020-11-22 00:51

    There is some indirect way for forcing garbage collector. You just need to fill heap with temporary objects until the point when garbage collector will execute. I've made class which forces garbage collector in this way:

    class GarbageCollectorManager {
    
        private static boolean collectionWasForced;
        private static int refCounter = 0;
    
        public GarbageCollectorManager() {
            refCounter++;
        }
    
        @Override
        protected void finalize() {
            try {
                collectionWasForced = true;
                refCounter--;
                super.finalize();   
            } catch (Throwable ex) {
                Logger.getLogger(GarbageCollectorManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public int forceGarbageCollection() {
            final int TEMPORARY_ARRAY_SIZE_FOR_GC = 200_000;
            int iterationsUntilCollected = 0;
            collectionWasForced = false;
    
            if (refCounter < 2) 
                new GarbageCollectorManager();
    
            while (!collectionWasForced) {
                iterationsUntilCollected++;
                int[] arr = new int[TEMPORARY_ARRAY_SIZE_FOR_GC];
                arr = null;
            }
    
            return iterationsUntilCollected;
        }
    
    }
    

    Usage:

    GarbageCollectorManager manager = new GarbageCollectorManager();
    int iterationsUntilGcExecuted = manager.forceGarbageCollection();
    

    I don't know how much this method is useful, because it fills heap constantly, but if you have mission critical application which MUST force GC - when this may be the Java portable way to force GC.

    0 讨论(0)
提交回复
热议问题