Examples and practices for the talk on JVM Memory Management basics given to my colleagues at Perficient in August 2022
Most of the examples are written with jbang
There are multiple ways to install jbang
on your machine, but here are the options in case
you don't want to install it.
-
Powershell:
iex "& { $(iwr https://ps.jbang.dev) } [jbang arguments]"
-
GitBash/cygwin/mingwin/WSL:
curl -ls https://sh.jbang.dev | bash -s - [jbang arguments]
The following oneliners assume that you have the
idea
binary in your$PATH
. Ifvscode
is your preferred editor, you can use--open=code
instead
jbang edit --open=idea StackAndHeap@garodriguezlp/jvm-memory-management
If you want to know what's
jbang
doing under the hood, you can use the--verbose
flagOther java versions could be chosen by using the
--java
flag. Example:--java 11
. See jbang docs
-
Running it with Java 8
jbang run ` --java 8 ` --java-options='-verbose:gc' ` IntroGarbageCollection@garodriguezlp/jvm-memory-management
-
Running it with Java 8 limiting max heap, then run it with
--java 11
and see the difference after the last GC. The thing here is that GC in Java 11 is capable of giving back memory to the OSjbang run ` --java 8 ` --java-options='-verbose:gc' ` --java-options='-Xmx127m' ` IntroGarbageCollection@garodriguezlp/jvm-memory-management
-
Running it with Java 8 limiting max and min heap
jbang run ` --java 8 ` --java-options='-verbose:gc' ` --java-options='-Xms127m' ` --java-options='-Xmx127m' ` IntroGarbageCollection@garodriguezlp/jvm-memory-management
-
Running
visualvm
could be executed like this if$JAVA_HOME
and$VISUALVM_HOME
environment variables are properly set (install the Visual GC plugin first)& $env:VISUALVM_HOME\bin\visualvm.exe --jdkhome $env:JAVA_HOME --console suppress
-
Run the following
jbang
powered classjbang run ` --java 11 ` --java-options='-verbose:gc' ` --java-options='-Xmx256m' ` --java-options='-XX:+HeapDumpOnOutOfMemoryError' ` MemoryLeak@garodriguezlp/jvm-memory-management
-
Open
visualvm
and connect to the process (don't forget to install the Visual GC plugin)& $env:VISUALVM_HOME\bin\visualvm.exe --jdkhome $env:JAVA_HOME --console suppress
-
Open Eclipse Memory Analyzer tool and load the heap dump file
& $env:MAT_HOME\eclipsec.exe