Sub topic 10
Making the image smaller
This chapter assumes you can already build an image with jlink --module-path ... --add-modules ... --output .... It also assumes you know how jdeps reports the modules your application needs. Here you will trim the image in three ways: by removing files, by shrinking the files that remain, and by choosing fewer modules.
Where the size comes from
A runtime image is a directory. Its size has three main parts:
- The modules. Each module in the image adds classes, and some add native libraries. This is usually the largest part.
- Extras around the classes. Debug information, C header files, and man pages are useful to JDK developers but not to a program that only runs.
- Storage format. The classes live in a single file,
lib/modules. It can be stored uncompressed or compressed.
The options in this chapter target the second and third parts. Module selection targets the first.
Measure first
Do not guess. Build a baseline image with no trimming options, then record its size. This script uses the application from earlier chapters.
rm -rf build/baseline
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/baseline
du -sh build/baseline
du -sh build/baseline/*The second du shows which top-level directory is largest. On a typical JDK, lib dominates because it holds modules and the native libraries. Your numbers depend on the JDK version, vendor, and platform, so compare your own builds with each other instead of with figures from a blog.
jlink refuses to write into an existing output directory. That is why the script runs rm -rf first. Use a build directory that you can safely delete.
Removing files you do not need
Three options remove content without changing how the program behaves.
| Option | What it removes | Trade-off |
|---|---|---|
| `--strip-debug` | Debug information from the classes (line numbers, local variable names) and native debug symbols where the platform supports it | Stack traces no longer show source line numbers, and some profilers and debuggers show less detail |
| `--no-header-files` | The C header files under `include` | None for a program that does not compile native code against the image |
| `--no-man-pages` | The man pages under `man` | None at runtime |
--strip-debug is the one that changes visible behavior. A stack trace from a stripped image shows Main.java but not a line number. It looks like this:
Exception in thread "main" java.lang.IllegalStateException: boom
at com.example.app/com.example.app.Main.main(Unknown Source)That is a real cost when you read production logs. Strip debug information from the JDK's classes if you want, but keep it for your own application. You can do this by building your own JAR with debug attributes and not relying on --strip-debug for it. The flag applies to every module in the image, so if you need line numbers for your code, decide whether the saved space is worth it.
A combined build looks like this:
rm -rf build/small
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 \
--output build/small
du -sh build/baseline build/smallCompressing the image
The --compress option controls how lib/modules is stored. The accepted values changed across JDK versions.
- JDK 21 and later: use
--compress=zip-0through--compress=zip-9.zip-0means no compression andzip-6is the usual middle choice. - JDK 11 through 20: use
--compress=0,1, or2. The value1shares constant strings, and2applies ZIP compression. - JDK 21 and later: the numeric values still work but print a deprecation warning.
Check your JDK with jlink --help, because the option text there is authoritative for your build.
rm -rf build/compressed
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--strip-debug --no-header-files --no-man-pages \
--compress=zip-6 \
--output build/compressed
du -sh build/small build/compressedCompression is not free. The JVM decompresses classes as it loads them, so startup can be slightly slower. Compression helps most when the image is stored or transferred, for example in a container registry, and least when startup time matters. Measure both the size and the startup time of your own program before choosing a level.
Choosing fewer modules
Removing files saves megabytes. Removing modules can save much more, and it is the part you control through your own dependency choices.
Start from what jdeps reports for your application, not from a hand-written guess:
jdeps --print-module-deps mods/app.jarThen add only those modules, plus your own. With a modular application, --add-modules com.example.app already pulls in everything the descriptor requires, so you rarely need to list JDK modules yourself.
Some modules enlarge the image more than their names suggest:
jdk.localedatacarries locale data beyond English and a few other locales. If your application needs only some locales, use--include-locales=en,deinstead of including all of them. This option requires thejdk.localedatamodule to be in the image.java.desktopis large, andjava.sqlpulls injava.xmlandjava.logging. If a transitiverequiresbrings in a module you do not use, look for the dependency in your own descriptor.jdk.unsupportedappears injdepsoutput when code usessun.misc.Unsafe. You need it in that case, but it is a signal to check which library is responsible.
To see what an image contains, list its modules and count them:
build/compressed/bin/java --list-modules
build/compressed/bin/java --list-modules | wc -lIf you see a module you did not expect, find out why it is there. java --describe-module java.sql on the image shows what a module requires. The unexpected module is usually reached through a requires in a library module, or from --bind-services (covered in a later chapter), which adds service providers on purpose.
Other options worth knowing
--vm=serverkeeps only the server JVM. Other values (client,minimal,all) exist, but which are available depends on the JDK build and platform. Checkjlink --helpbefore relying on them.--generate-cds-archive(JDK 13 and later) adds a class-data-sharing archive that speeds startup. It makes the image larger. It is a trade of size for startup time and the opposite of the options above.jlink --list-pluginsprints every plugin and option in your JDK, which is useful because the available set changes between releases.
A reproducible comparison script
This script builds the three variants and prints their sizes in kilobytes, so you can see the effect of each step on your own machine. Run it from the project root after the mods/app.jar step from earlier chapters.
#!/usr/bin/env bash
set -euo pipefail
MP="$JAVA_HOME/jmods:mods"
rm -rf build
mkdir -p build
jlink --module-path "$MP" --add-modules com.example.app \
--output build/baseline
jlink --module-path "$MP" --add-modules com.example.app \
--strip-debug --no-header-files --no-man-pages \
--output build/trimmed
jlink --module-path "$MP" --add-modules com.example.app \
--strip-debug --no-header-files --no-man-pages --compress=zip-6 \
--output build/compressed
du -sk build/baseline build/trimmed build/compressedIf your JDK is older than 21, replace --compress=zip-6 with --compress=2. Run the image after each build (build/compressed/bin/java -m com.example.app/com.example.app.Main) to confirm that trimming did not break it.
Summary
Start with a baseline and measure. Add --strip-debug, --no-header-files, and --no-man-pages for a low-risk reduction, and know that stripped stack traces lose line numbers. Add --compress when disk or transfer size matters more than a small startup cost. Then review the module list, because dropping an unneeded module saves more than any flag.
Exercise
A teammate builds an image with --strip-debug and --compress=zip-6 and reports that the image is smaller but that production stack traces no longer show line numbers. Which option caused that, and what would you check before deciding whether to keep it?
Show answer
--strip-debug removed the debug attributes that carry line numbers. --compress only changes how lib/modules is stored and does not affect stack traces. Before keeping the option, weigh the saved megabytes (measure with du against an image built without it) against how much you rely on line numbers when reading production logs. If you need them, build without --strip-debug.
Exercise
You run java --list-modules on your finished image and see jdk.localedata, although your application only formats text in English and German. How can you reduce the image, and which option do you use?
Show answer
Keep jdk.localedata only if you need locales beyond the default set, and restrict it with --include-locales=en,de. This requires jdk.localedata to be in the image, but jlink then keeps only the listed locales. If you do not need any extra locale data, find which module or --bind-services setting added jdk.localedata and remove it, then rebuild and run list-modules again to confirm.