/* * TestMemory.java * * Created on 30 November 2001, 14:03 * Last updated on 09 November 2007 * * Copyright (C) 2007 Andy Turner, University of Leeds, UK. * * 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 library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package uk.ac.leeds.ccg.andyt.tests.java; /** * @author Andy Turner * @author Paul Townend * @version 0.2 */ public class TestMemory { private Runtime _Runtime; private long maxMemory; private long allocatedMemory; private long freeMemory; public TestMemory(){ _Runtime = java.lang.Runtime.getRuntime(); } /** * @param args the command line arguments */ public static void main(String args[]) { TestMemory _TestMemory = new TestMemory(); _TestMemory.run(); } public void run() { //printMemoryInformation(); System.gc(); System.out.println("Garbage Collect"); //printMemoryInformation(); // Set some longs for storing amount of available memory //int _int0 = 7734751; //Max for Netbeans normal runs; int _int0 = 7734670; //Max for Netbeans debug runs; int _int1 = 0; double[] _doubleArray; int[] _intArray; byte[] _byteArray; int _intZero = 0; //long _long0 = 10240L; //long _long1 = 0L; try { do { System.gc(); _doubleArray = new double[_int0]; _int0 += 1; } while ( true ); } catch ( OutOfMemoryError _OutOfMemoryError ) { System.gc(); System.out.println("_int0 " + _int0 ); _int0 -= 1; _doubleArray = new double[_int0]; System.gc(); printMemoryInformation(); try { System.out.println("What is left?"); // Obviously enough to print this! _int0 = 1048572; //Max for Netbeans normal runs; do { System.gc(); _intArray = new int[ _int0 ]; for ( _int1 = _intZero; _int1 < _int0; _int1 ++ ) { _intArray[ _int1 ] = _int1; } System.gc(); //System.out.println( _int0 ); //printMemoryInformation(); _int0 ++; } while ( true ); } catch ( OutOfMemoryError _OutOfMemoryError2 ) { System.gc(); _int0 --; System.out.println("_int0 " + _int0 ); _intArray = new int[ _int0 ]; for ( _int1 = _intZero; _int1 < _int0; _int1 ++ ) { _intArray[ _int1 ] = _int1; } System.gc(); printMemoryInformation(); try { System.out.println("What is left?"); // Obviously enough to print this! System.gc(); _int0 = 458739; System.gc(); do { System.gc(); _byteArray = new byte[ _int0 ]; for ( _int1 = _intZero; _int1 < _int0; _int1 ++ ) { _byteArray[ _int1 ] = Byte.MAX_VALUE; } System.gc(); //System.out.println( _int0 ); //printMemoryInformation(); _int0 ++; } while ( true ); } catch ( OutOfMemoryError _OutOfMemoryError3 ) { System.gc(); _int0 --; System.out.println("_int0 " + _int0 ); _byteArray = new byte[ _int0 ]; for ( _int1 = _intZero; _int1 < _int0; _int1 ++ ) { _byteArray[ _int1 ] = Byte.MAX_VALUE; } System.gc(); //printMemoryInformation(); // Not enough memory to print this! try { System.gc(); printMemoryInformation2(); // Not enough memory to print this! } catch ( OutOfMemoryError _OutOfMemoryError4 ) { try { // More memory can be allocated here int _int3 = 0; // Despite not seeming to have any memory accroding to _Runtime, we can allocate this. System.out.println( _int3 ); System.out.println( "Finish 0" ); } catch ( OutOfMemoryError _OutOfMemoryError5 ) { System.out.println( "Finish 1" ); } } } } } } /** * @Return The TotalFreeMemory available as calculated from _Runtime. */ protected long getTotalFreeMemory() { long result; maxMemory = _Runtime.maxMemory(); allocatedMemory = _Runtime.totalMemory(); freeMemory = _Runtime.freeMemory(); result = ( freeMemory + ( maxMemory - allocatedMemory ) ) / 1024; return result; } /** * Prints out Memory Information. */ protected void printMemoryInformation() { maxMemory = _Runtime.maxMemory(); allocatedMemory = _Runtime.totalMemory(); freeMemory = _Runtime.freeMemory(); System.out.println("free memory: " + freeMemory / 1024); System.out.println("allocated memory: " + allocatedMemory / 1024); System.out.println("max memory: " + maxMemory / 1024); System.out.println("total free memory: " + (freeMemory + (maxMemory - allocatedMemory)) / 1024); } /** * Prints out Memory Information. */ protected void printMemoryInformation2() { maxMemory = _Runtime.maxMemory(); allocatedMemory = _Runtime.totalMemory(); freeMemory = _Runtime.freeMemory(); System.out.println(freeMemory); System.out.println(allocatedMemory); System.out.println(maxMemory); System.out.println(freeMemory + (maxMemory - allocatedMemory)); } }