Skip to content

Instantly share code, notes, and snippets.

@vicziani
Created January 1, 2012 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicziani/1548642 to your computer and use it in GitHub Desktop.
Save vicziani/1548642 to your computer and use it in GitHub Desktop.
OutOfMemory puffer teszt
/*
* Egy példaprogram arra, hogy működik-e az a trükk, hogy lefoglalunk egy kis
* helyet, és OutOfMemoryError esetén felszabadítjuk.
*
* Igen. -verbose:gc kapcsolóval futtatva látszik, hogy egy Full GC-t lefuttat a
* catch ágban is.
*/
package jtechlog.test;
public class TestOutOfMemory {
private static byte[] MEGA_ARRAY = new byte[2 * 1024 * 1024];
public static void main(String[] args) throws InterruptedException {
System.out.println("Start.");
byte[][] a = new byte[128 * 1204][];
int i = 0;
try {
for (i = 0; i < a.length; i++) {
a[i] = new byte[1024* 1024];
System.out.format("[%s]\r", i);
Thread.sleep(100);
}
}
catch (OutOfMemoryError oome) {
System.out.println("\n");
System.out.println("Out of memory, new allocation...");
// Ez kihagyva újabb OutOfMemoryError, viszont beleírva le
// tudja foglalni az új tömböt.
MEGA_ARRAY = null;
byte[] b = new byte[1024 * 1024];
System.out.println(b.length);
System.out.println("End of new allocation");
}
System.out.println("End.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment