Last active
April 6, 2021 00:42
-
-
Save btnguyen2k/fe9a06c82b355f06a95bd0d612500a0b to your computer and use it in GitHub Desktop.
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.*; | |
/** | |
* A demo Java application to see how GC works. | |
* | |
* @author Thanh Nguyen | |
*/ | |
public class GcMemDemo { | |
static Map<Long, byte[]> buffer = new HashMap<>(); | |
static String format(double bytes, int digits) { | |
String[] dictionary = { "bytes", "KB", "MB" }; | |
int index = 0; | |
for (index = 0; index < dictionary.length-1; index++) { | |
if (bytes < 1024) { | |
break; | |
} | |
bytes = bytes / 1024; | |
} | |
return String.format("%." + digits + "f", bytes) + " " + dictionary[index]; | |
} | |
static void printMemInfo() { | |
Runtime runtime = Runtime.getRuntime(); | |
System.out.println("Memory info:"); | |
long total = runtime.totalMemory(); | |
long free = runtime.freeMemory(); | |
long freePercent = (free*100/total); | |
System.out.println("\tUsed : " + buffer.size() + " - " + format(buffer.size()*1024*1024, 2)); | |
System.out.println("\tMax : " + format(runtime.maxMemory(), 2)); | |
System.out.println("\tTotal: " + format(total, 2)); | |
System.out.println("\tFree : " + format(free, 2) + " ("+freePercent+"%)"); | |
} | |
static void printMemInfo(long beforeTotal, long beforeFree) { | |
Runtime runtime = Runtime.getRuntime(); | |
long beforeFreePersent = (beforeFree*100/beforeTotal); | |
long afterTotal = runtime.totalMemory(); | |
long afterFree = runtime.freeMemory(); | |
long afterFreePersent = (afterFree*100/afterTotal); | |
System.out.println("Memory info:"); | |
System.out.println("\tUsed : " + buffer.size() + " - " + format(buffer.size()*1024*1024, 2)); | |
System.out.println("\tMax : " + format(runtime.maxMemory(), 2)); | |
System.out.println("\tTotal: " + format(beforeTotal, 2) + " --> " + format(afterTotal, 2)); | |
System.out.println("\tFree : " + format(beforeFree, 2) + " ("+beforeFreePersent+"%) --> " + format(afterFree, 2) + " ("+afterFreePersent+"%)"); | |
} | |
static void printMenu() { | |
System.out.println("========================================"); | |
System.out.println("1. Print memory info"); | |
System.out.println("2. Allocate 100Mb memory"); | |
System.out.println("3. Dispose all allocated memory"); | |
System.out.println("4. Dispose and System.gc()"); | |
System.out.println("Q. Quit"); | |
System.out.println(); | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
boolean done = false; | |
String input = "1"; | |
while (!done) { | |
printMenu(); | |
System.out.print("input> "); | |
input = scanner.nextLine(); | |
if ("Q".equalsIgnoreCase(input)) { | |
done = true; | |
} else if ("1".equals(input)) { | |
printMemInfo(); | |
} else if ("2".equals(input)) { | |
Runtime runtime = Runtime.getRuntime(); | |
long beforeTotal = runtime.totalMemory(); | |
long beforeFree = runtime.freeMemory(); | |
long index = System.nanoTime(); | |
for ( int i = 0; i < 100; i++) { | |
buffer.put(index+i, new byte[1024*1024]); | |
} | |
printMemInfo(beforeTotal, beforeFree); | |
} else if ("3".equals(input)) { | |
Runtime runtime = Runtime.getRuntime(); | |
long beforeTotal = runtime.totalMemory(); | |
long beforeFree = runtime.freeMemory(); | |
buffer = new HashMap<>(); | |
printMemInfo(beforeTotal, beforeFree); | |
} else if ("4".equals(input)) { | |
Runtime runtime = Runtime.getRuntime(); | |
long beforeTotal = runtime.totalMemory(); | |
long beforeFree = runtime.freeMemory(); | |
buffer = new HashMap<>(); | |
System.gc(); | |
printMemInfo(beforeTotal, beforeFree); | |
} | |
} | |
} | |
} |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseG1GC -Xms16m -Xmx1234m GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseG1GC -Xms16m -Xmx1234m -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:-ShrinkHeapInSteps GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseParallelGC -Xms16m -Xmx1234m GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseParallelGC -Xms16m -Xmx1234m -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:-ShrinkHeapInSteps GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseSerialGC -Xms16m -Xmx1234m GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseSerialGC -Xms16m -Xmx1234m -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:-ShrinkHeapInSteps GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseShenandoahGC -Xms16m -Xmx1234m GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UseShenandoahGC -Xms16m -Xmx1234m -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:-ShrinkHeapInSteps GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xms16m -Xmx1234m GcMemDemo |
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
#!/bin/sh | |
rm *.class | |
java -version | |
javac GcMemDemo.java | |
java -server -XX:+ExitOnOutOfMemoryError -XX:+CrashOnOutOfMemoryError -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xms16m -Xmx1234m -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:-ShrinkHeapInSteps GcMemDemo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment