Created
July 26, 2012 10:48
-
-
Save ffbit/3181457 to your computer and use it in GitHub Desktop.
Simple example of JVM GC usage
This file contains 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
import java.util.LinkedList; | |
import java.util.Queue; | |
import java.util.Scanner; | |
public class GC { | |
private static Queue<Object> queue = new LinkedList<Object>(); | |
private static Scanner scanner = new Scanner(System.in); | |
public static void main(String...args) { | |
while (true) { | |
printCurrentState(); | |
printInvitation(); | |
processCommand(); | |
} | |
} | |
private static void printCurrentState() { | |
print("Current number of objects is: " + queue.size()); | |
} | |
private static void printInvitation() { | |
print("Add or remove some objects. Use +/-N"); | |
} | |
private static void processCommand() { | |
int number = scanner.nextInt(); | |
if (number > 0) { | |
increase(number); | |
} else { | |
decrease(Math.abs(number)); | |
} | |
} | |
private static void increase(int number) { | |
for (int i = 0; i < number; i++) { | |
queue.add(new Object()); | |
} | |
} | |
private static void decrease(int number) { | |
for (int i = 0; i < number; i++) { | |
queue.poll(); | |
} | |
} | |
private static void print(String message) { | |
System.out.println(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment