import java.io.IOException; import java.util.Vector; /** * Created by moshee * on: 07/06/17 * to compile in place: `javac ConsumeHeap.java` * Execute: `java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/app-`date +%s`-pid$$.hprof -XX:OnOutOfMemoryError=/opt/app/bin/upload_dump_s3.sh -Xmx2m ConsumeHeap` * HeapDumpOnOutOfMemoryError specifies to automatically create a dump when OOM occures * HeapDumpPath supplies a path to put that file * OnOutOfMemoryError specifies a script(s) to run after dump is created (in this case upload_dump_s3.sh) */ public class ConsumeHeap { public static void main(String[] args) throws IOException { Vector v = new Vector(); while (true) { byte b[] = new byte[1048576]; v.add(b); Runtime rt = Runtime.getRuntime(); System.out.println( "free memory: " + rt.freeMemory() ); } } }