Sub topic 10
Making the image smaller
This chapter assumes you can already build a basic image with jlink and know what --add-modules and --output do. It covers the options that shrink the image and shows how to measure each one.
Where the size comes from
An image holds three kinds of content. Each has its own way to reduce it.
| Content | How to reduce it |
|---|---|
| Modules | Add fewer modules with `--add-modules` |
| Extra files | `--no-header-files`, `--no-man-pages` |
| Class and debug data | `--strip-debug`, `--compress` |
Start with the module list
The biggest saving is leaving out modules you do not need. Every module you add brings its classes and its own dependencies. Use jdeps --print-module-deps to list what your JAR requires. Add only those modules, plus any you need for services or reflection.
Remove files you will not use
Two options drop files that only developers use:
--no-header-filesleaves out the C header files used for native code (JNI) development.--no-man-pagesleaves out the manual pages for the tools inbin.
Neither option changes how your application runs.
Strip debug information
--strip-debug removes debugging information from class files and from native libraries where the platform allows it. Stack traces still show method names. They can lose local variable names and some line details, which makes debugging a production problem harder. Keep an unstripped build for diagnosis.
Compress the image
--compress shrinks the stored class and resource data. The syntax depends on your JDK version:
- JDK 21 and later:
--compress=zip-0through--compress=zip-9. Levelzip-6is a common balance. - Earlier versions:
--compress=0(none),1(shares constant strings) or2(ZIP).
Compression costs a little startup time because the runtime must decompress classes when it loads them.
Build and measure
This script builds an image with no reductions, then one with all of them, and compares their sizes. It assumes the mods directory from the earlier chapters and a JDK 21 or later.
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/runtime-plain
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--compress=zip-6 \
--output build/runtime-small
du -sh build/runtime-plain build/runtime-smallExact sizes depend on your JDK vendor, version and platform. Measure your own build instead of relying on numbers from elsewhere.
Common mistakes
- Adding
java.seto be safe. It pulls in most of the platform and removes most of your savings. - Stripping debug data and then being unable to read a production stack trace. Keep an unstripped image for diagnosis.
- Raising the compression level without testing startup time.
Exercise
Your image is still large after you add --strip-debug, --no-header-files and --no-man-pages. What should you check first, and why?
Show answer
Check the modules passed to --add-modules, for example with jdeps --print-module-deps. Modules usually account for most of the image size, so an unneeded module such as java.se saves more when removed than all three file-trimming options combined.