Java Memory waitForMemory(double memory)

Here you can find the source of waitForMemory(double memory)

Description

Wait till the given requested amount of MegaBytes is free in memory

License

Open Source License

Parameter

Parameter Description
memory in MegaBytes units

Declaration

public static void waitForMemory(double memory) 

Method Source Code

//package com.java2s;

public class Main {
    public final static int KILO = 1024;

    /**//w  w w  .jav  a  2 s .  com
     * Wait till the given requested amount of MegaBytes is free in memory
     * @param memory   in MegaBytes units
     */
    public static void waitForMemory(double memory) {
        long memoryInBytes = ConvertMegaByteToByte(memory);

        while (Runtime.getRuntime().freeMemory() < memoryInBytes) {
            System.gc();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static long ConvertMegaByteToByte(double megaByteUnits) {
        return (long) (megaByteUnits * (KILO * KILO));
    }
}

Related

  1. reportMemory(double val)
  2. simpleMemory()
  3. testMemory(long bytesToTest)
  4. throwOutOfMemoryError()
  5. unlimitMemory()
  6. writeMemory()