Java Memory compactMemory()

Here you can find the source of compactMemory()

Description

Compacts memory as much as possible by allocating huge memory block and then forcing garbage collection.

License

Open Source License

Declaration

public static void compactMemory() 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w w.j a v  a  2 s  .  c om
     * Compacts memory as much as possible by allocating huge memory block
     * and then forcing garbage collection.
     */
    public static void compactMemory() {
        try {
            final byte[][] unused = new byte[128][];
            for (int i = unused.length; i-- != 0;) {
                unused[i] = new byte[2000000000];
            }
        } catch (OutOfMemoryError ignore) {
        }
        System.gc();
    }
}

Related

  1. alignMemory(long numBytes)
  2. checkLowMemory()
  3. checkMemory()
  4. checkMinMemory(long min)
  5. correctMemoryValue(int memory)
  6. displayMemory(String label, long bytes)
  7. dumpMemory()
  8. dumpMemoryInfo(String msg)