import java.util.Random; public class AllocObjects { static byte[] array; static Random rand = new Random(); public static void main(String[] args) { int iterations = 0; while (true) { if (++iterations % 100 == 0) { System.out.println("Iteration:" + iterations); } alloc(); } } static void alloc() { System.gc(); array = new byte[1024 * 1024]; array[rand.nextInt(1024 * 1024)] = (byte) rand.nextInt(); } }