Created
January 1, 2012 23:28
-
-
Save vicziani/1548642 to your computer and use it in GitHub Desktop.
OutOfMemory puffer teszt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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