Java Memory fillMemory()

Here you can find the source of fillMemory()

Description

Fills memory and returns an object that refers to all of what was allocated.

License

Open Source License

Declaration

public static Object fillMemory() 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .  j  a  va2s . com*/
 *
 *
 * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation. 
 * 
 * This program 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 version 2 for more details (a copy is
 * included at /legal/license.txt). 
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA 
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions. 
 */

public class Main {
    /**
     * Fills memory and returns an object that refers to all of what was 
     * allocated. The intent is that all subsequent allocations will cause 
     * OutOfMemoryError until the reference to the returned object is 
     * discarded.
     */
    public static Object fillMemory() {
        byte[][] arrays = new byte[100][];
        int size = 1 << 30;
        int idx = 0;

        while (true) {
            try {
                while (true) {
                    arrays[idx++] = new byte[size];
                    // p(size);
                }
            } catch (OutOfMemoryError oome) {
                if (size == 0) {
                    break;
                } else {
                    size /= 2;
                }
            }
        }
        return arrays;
    }
}

Related

  1. compactMemory()
  2. correctMemoryValue(int memory)
  3. displayMemory(String label, long bytes)
  4. dumpMemory()
  5. dumpMemoryInfo(String msg)
  6. isDefaultMemoryCardDrive(String aRoot)
  7. isInMemoryDatabase(final String databasePath)
  8. isMemoryAccess(final String value)
  9. isMemoryData(String metric)