Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created July 26, 2012 10:48
Show Gist options
  • Save ffbit/3181457 to your computer and use it in GitHub Desktop.
Save ffbit/3181457 to your computer and use it in GitHub Desktop.
Simple example of JVM GC usage
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