Java Memory Free belowMemoryLimit(int percentFree)

Here you can find the source of belowMemoryLimit(int percentFree)

Description

below Memory Limit

License

Open Source License

Declaration

public static boolean belowMemoryLimit(int percentFree) 

Method Source Code

//package com.java2s;
/**/*ww w .ja v a  2 s  . c o  m*/
 * Copyright (C) 2009 G?nter Ladwig (gla at aifb.uni-karlsruhe.de)
 * 
 * This file is part of the graphindex project.
 *
 * graphindex is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2
 * as published by the Free Software Foundation.
 * 
 * graphindex 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 graphindex.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static boolean belowMemoryLimit(int percentFree) {
        long max = Runtime.getRuntime().maxMemory() / 1000;
        long free = freeMemory();

        if ((double) free / max * 100 < percentFree)
            return true;
        return false;
    }

    public static long freeMemory() {
        Runtime r = Runtime.getRuntime();
        return (r.freeMemory() + (r.maxMemory() - r.totalMemory())) / 1000;
    }
}

Related

  1. checkFreeMemory(long requiredMemory)
  2. formatMemorySize(Long afreeDiskSpace)
  3. freeAllocatedMemory()
  4. freeMemory()