Last active
August 29, 2015 14:12
-
-
Save kedarmhaswade/c88897dbea8ed51a6847 to your computer and use it in GitHub Desktop.
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
/** Studies Java Memory */ | |
import java.util.*; | |
import java.io.*; | |
public class JavaMemory { | |
static volatile boolean stop = false; | |
public static void main(String[] args) throws Exception { | |
System.out.println("Hope you ran it with -Xmx32m"); | |
Thread alloc = new Thread(new Allocator()); | |
alloc.start(); | |
} | |
} | |
class Allocator implements Runnable { | |
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
volatile long[] longs = new long[0]; | |
public void run() { | |
while (!JavaMemory.stop) { | |
String s = null; | |
try { | |
s = reader.readLine(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
if ("n".equals(s)) { | |
//System.gc(); | |
longs = new long[longs.length+1_000_000]; | |
System.out.println("allocated new long array, size: " + longs.length); | |
} else if ("q".equals(s)) { | |
JavaMemory.stop = true; | |
} else { | |
//ignore ? | |
System.out.println("press 'n' or 'q'"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment