Skip to content

Instantly share code, notes, and snippets.

@goyusia
Created November 28, 2015 13:09
Show Gist options
  • Select an option

  • Save goyusia/7db2461ab4d953ee2efc to your computer and use it in GitHub Desktop.

Select an option

Save goyusia/7db2461ab4d953ee2efc to your computer and use it in GitHub Desktop.
Sheep count with Garbage Collector
/*
How to run
$ javac Sheep.java && java Sheep -Xms1k -Xmx1k -XX:MaxPermSize=1k -XX:MaxNewSize=1k
Sample output
양(id=35994) is created
양(id=35995) is created
양(id=35996) is created
양(id=17679) is killed, current alive 양 14956마리
양(id=35997) is created
양(id=17678) is killed, current alive 양 14967마리
양(id=17677) is killed, current alive 양 14967마리
양(id=17676) is killed, current alive 양 14966마리
양(id=17675) is killed, current alive 양 14965마리
*/
class Sheep {
private static long nextId = 0;
private static long aliveCounter = 0;
private long id;
public Sheep() {
aliveCounter += 1;
nextId += 1;
this.id = nextId;
String msg = String.format("양(id=%d) is created", this.id);
System.out.println(msg);
}
@Override
protected void finalize() throws Throwable {
aliveCounter -= 1;
String msg = String.format("양(id=%d) is killed, current alive 양 %d마리", this.id, aliveCounter);
System.out.println(msg);
super.finalize();
}
public static void main(String[] args) {
for(int i = 0 ; i < 100000 ; ++i) {
new Sheep();
}
}
}
@goyusia

goyusia commented Nov 28, 2015

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment