Java Memory memoryIsLow()

Here you can find the source of memoryIsLow()

Description

Returns true if less then 5% of the available memory is free.

License

Open Source License

Return

true if less then 5% of the available memory is free.

Declaration

public static boolean memoryIsLow() 

Method Source Code

//package com.java2s;
/*       //from   w  w  w  . ja va2s.c  o  m
 * DSI utilities
 *
 * Copyright (C) 2002-2009 Sebastiano Vigna 
 *
 *  This library is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as published by the Free
 *  Software Foundation; either version 2.1 of the License, or (at your option)
 *  any later version.
 *
 *  This library 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 Lesser General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

public class Main {
    private final static Runtime RUNTIME = Runtime.getRuntime();

    /** Returns true if less then 5% of the available memory is free.
     * 
     * @return true if less then 5% of the available memory is free.
     */
    public static boolean memoryIsLow() {
        return availableMemory() * 100 < RUNTIME.totalMemory() * 5;
    }

    /** Returns the amount of available memory (free memory plus never allocated memory).
     * 
     * @return the amount of available memory, in bytes.
     */
    public static long availableMemory() {
        return RUNTIME.freeMemory() + (RUNTIME.maxMemory() - RUNTIME.totalMemory());
    }
}

Related

  1. memory()
  2. memory()
  3. memory()
  4. memoryEqual(byte[] left, int leftOffset, byte[] right, int rightOffset, int length)
  5. memoryInfo()
  6. memoryOccupied()
  7. memorySize(long bytesSize)
  8. memoryUtilization()
  9. memoryValue(long inBytes)