Created
November 28, 2015 13:09
-
-
Save goyusia/7db2461ab4d953ee2efc to your computer and use it in GitHub Desktop.
Sheep count with Garbage Collector
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
| /* | |
| 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(); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twitter.com/if1live/status/670583486651076608