Here you can find the source of calculateMemory()
public static int calculateMemory()
//package com.java2s; //License from project: Open Source License import java.util.concurrent.atomic.AtomicBoolean; public class Main { private static AtomicBoolean memory = new AtomicBoolean(false); public static int calculateMemory() { long heapSize = Runtime.getRuntime().totalMemory(); long heapMaxSize = Runtime.getRuntime().maxMemory(); if (heapSize < heapMaxSize) { return Integer.MAX_VALUE; }//from w w w. ja va2 s.c om long heapFreeSize = Runtime.getRuntime().freeMemory(); int size = (int) ((heapFreeSize * 100) / heapMaxSize); if (size > (100 - 95)) { memoryPlentifulTask(); return Integer.MAX_VALUE; } return size; } public static void memoryPlentifulTask() { memory.set(false); } }