Java Memory Free freeMemory()

Here you can find the source of freeMemory()

Description

Utility to free memory

License

Open Source License

Declaration

public final static void freeMemory() 

Method Source Code

//package com.java2s;
/**/*  w w w  .  j a  v  a  2 s .  c  om*/
   This file is part of Vitam Project.
    
   Copyright 2009, Frederic Bregier, and individual contributors by the @author
   tags. See the COPYRIGHT.txt in the distribution for a full listing of
   individual contributors.
    
   All Vitam Project is free software: you can redistribute it and/or 
   modify it under the terms of the GNU General Public License as published 
   by the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   Vitam is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with Vitam .  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Utility to free memory
     */
    public final static void freeMemory() {
        long total = Runtime.getRuntime().totalMemory();
        for (int i = 0; i < 10; i++) {
            System.gc();
        }
        while (true) {
            System.gc();
            long newtotal = Runtime.getRuntime().totalMemory();
            if ((((double) (total - newtotal)) / (double) total) < 0.1) {
                break;
            } else {
                total = newtotal;
            }
        }
        System.gc();
    }
}

Related

  1. formatMemorySize(Long afreeDiskSpace)
  2. freeAllocatedMemory()
  3. freeMemory()
  4. freeMemory()
  5. freeMemory()
  6. freeMemory()
  7. freeMemorym()
  8. freeMemoryMB()
  9. freeMemoryPercents()