Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can I force full garbage collection?

paultaylorJun 27 2008 — edited Jun 27 2008
Hi, my program is memory bound , as users load more files more memory is required , if the user decides to load a very large number of files they will eventually run out of memory. So I am trying to detect when there is less 15% of heap memory left, then force garbage collection and if it cant free up more than 15% of the heap I will stop the user from loading any more files. But the problem is though I call System.gc() to try and force a full garbage collect it rarely
retrieve enough memory to get below the 15% limit. But using the Yourkit Profiler I can select the Force GarbageCollection option and this always manages to free up more memory to get the figure under the 15% limit. in support of this I found that sometimes my program stop me loading more files when there is still quite a bit available.

So my questions are.
1. I know System.gc() is only hint to garbage collect, but the docs imply it only replys after the garbage collection (if any) has been done, is this right or do I have to wait.
2. Is there any way to Force complete Garbage Collectionas profiler appears to do.
3. is there a VM option I could set instead to force the JVM to completely garbage collect at say 83% so that if I then polled that 85% of heap was being used I would know that it really was, and I wouldnt need to bother trying to garbage collect further. (Im using Suns 1.6. JVM on Windows and Linux, and Apples 1.5 or 1.6 JVM on Macs)
public static void checkMemoryWhilstLoadingFiles() throws LowMemoryException
    {
        MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        //No max defined future proofing
        if(mu.getMax()==-1)
        {
            return;
        }
        if (mu.getUsed() > (mu.getMax()  *  0.85f))
        {
            MainWindow.logger.warning("Memory low:" + mu);
            System.gc();
            MainWindow.logger.warning("Memory low gc1:" + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
            System.gc();
            MainWindow.logger.warning("Memory low gc2:" + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
            System.gc();
            MainWindow.logger.warning("Memory low gc3:" + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
            mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
            if (mu.getUsed()  > (mu.getMax()  *  0.85f))
            {
                MainWindow.logger.severe("Memory too low:" + mu);
                throw new LowMemoryException("Running out of memory:"+mu.getUsed());
            }
            else
            {
                 MainWindow.logger.warning("Memory usage reduced to:" + mu);    
            }
        }
    }
thanks for any help Paul

Edited by: paultaylor on 27-Jun-2008 11:10
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 25 2008
Added on Jun 27 2008
2 comments
552 views