Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

Memory: the app and the scan engine stay near what they use, and give memory back

scottsturrockeef6a6b0fb48
diff --git a/package-appimage.sh b/package-appimage.sh
index 54907cb..7eac8b5 100755
--- a/package-appimage.sh
+++ b/package-appimage.sh
@@ -61,7 +61,7 @@ echo ">> jpackage: self-contained app image using that runtime"
jpackage --type app-image --name OpenSuite \
--input build/otin --main-jar "$JARNAME" --main-class opensuite.ui.Main \
--icon src/opensuite/ui/icons/opensuite_256.png \
- --runtime-image build/runtime --java-options "-XX:MaxRAMPercentage=50 -XX:+UseG1GC -XX:G1PeriodicGCInterval=300000" \
+ --runtime-image build/runtime --java-options "-XX:MaxRAMPercentage=50 -XX:+UseG1GC -Xms64m -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30 -XX:G1PeriodicGCInterval=15000" \
--dest build/appimage
echo ">> assembling AppDir (with a scale-probing launcher)"
@@ -115,6 +115,8 @@ if [ -r "$HOME/.opensuite/memory-mb" ]; then
MB="$(tr -cd '0-9' < "$HOME/.opensuite/memory-mb" | cut -c1-7)"
if [ -n "$MB" ] && [ "$MB" -ge 256 ]; then export JAVA_TOOL_OPTIONS="${JAVA_TOOL_OPTIONS:+$JAVA_TOOL_OPTIONS }-Xmx${MB}m"; fi
fi
+# The system allocator otherwise keeps a pool of memory per thread, hundreds of megabytes never given back.
+export MALLOC_ARENA_MAX="${MALLOC_ARENA_MAX:-2}"
exec "$HERE/usr/bin/OpenSuite" "$@"
RUN
chmod +x AppDir/AppRun
diff --git a/package-macos.sh b/package-macos.sh
index 5bc6f5b..3395847 100755
--- a/package-macos.sh
+++ b/package-macos.sh
@@ -65,7 +65,7 @@ echo ">> jpackage: OpenSuite.app"
jpackage --type app-image --name OpenSuite --app-version "$VERSION" \
--input "$OUT/in" --main-jar "$JARNAME" --main-class opensuite.ui.Main \
--icon "$OUT/OpenSuite.icns" --runtime-image "$OUT/runtime" \
- --java-options "-XX:MaxRAMPercentage=50 -XX:+UseG1GC -XX:G1PeriodicGCInterval=300000" \
+ --java-options "-XX:MaxRAMPercentage=50 -XX:+UseG1GC -Xms64m -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30 -XX:G1PeriodicGCInterval=15000" \
--mac-package-identifier io.github.scottsturrock.opensuite --mac-package-name "Open Suite" \
--dest "$OUT"
APP="$OUT/OpenSuite.app"
diff --git a/package-windows.sh b/package-windows.sh
index 3946dfb..3fa69c4 100755
--- a/package-windows.sh
+++ b/package-windows.sh
@@ -56,7 +56,8 @@ jpackage --type app-image --name OpenSuite --app-version "$VERSION" \
--input "$OUT/in" --main-jar "$JARNAME" --main-class opensuite.ui.Main \
--icon icon/opensuite.ico --runtime-image "$OUT/runtime" \
--java-options "-XX:MaxRAMPercentage=50" --java-options "-XX:+UseG1GC" \
- --java-options "-XX:G1PeriodicGCInterval=300000" \
+ --java-options "-Xms64m" --java-options "-XX:MinHeapFreeRatio=10" --java-options "-XX:MaxHeapFreeRatio=30" \
+ --java-options "-XX:G1PeriodicGCInterval=15000" \
--vendor "Open Suite" --description "Open Suite" \
--dest "$OUT"
APP="$OUT/OpenSuite"
diff --git a/run.sh b/run.sh
index ed7e390..b815e14 100755
--- a/run.sh
+++ b/run.sh
@@ -63,4 +63,5 @@ if [ -r "$HOME/.opensuite/memory-mb" ]; then
MB="$(tr -cd '0-9' < "$HOME/.opensuite/memory-mb" | cut -c1-7)"
if [ -n "$MB" ] && [ "$MB" -ge 256 ]; then MEMORY=("-Xmx${MB}m"); fi
fi
-exec java "${MEMORY[@]}" -XX:+UseG1GC -XX:G1PeriodicGCInterval=300000 "${ARGS[@]}" -jar "$DIR/dist/opensuite.jar" "$@"
+export MALLOC_ARENA_MAX="${MALLOC_ARENA_MAX:-2}"
+exec java "${MEMORY[@]}" -XX:+UseG1GC -Xms64m -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30 -XX:G1PeriodicGCInterval=15000 "${ARGS[@]}" -jar "$DIR/dist/opensuite.jar" "$@"
diff --git a/src/opensuite/engine/ZapEngine.java b/src/opensuite/engine/ZapEngine.java
index 736aff2..6f8de3a 100644
--- a/src/opensuite/engine/ZapEngine.java
+++ b/src/opensuite/engine/ZapEngine.java
@@ -81,6 +81,29 @@ public final class ZapEngine {
private Path home; // the engine's scratch directory, ours to clean up
private boolean hookInstalled; // one shutdown hook per engine, not per start
+ /**
+ * The Java options the engine is started with. Left to itself Java sizes the
+ * engine for the machine, not for the work: on a large machine a few minutes
+ * of traffic took it to two gigabytes holding a tenth of that, and it kept
+ * them. These keep it near what it is using and give memory back when it is
+ * idle. The limit is the one the engine's own launcher uses, a quarter of the
+ * machine, but no more than four gigabytes.
+ * -Dopensuite.engine.java="..." replaces them, for trying others.
+ */
+ static List<String> javaOptions() {
+ String given = System.getProperty("opensuite.engine.java");
+ if (given != null && !given.isBlank()) return List.of(given.trim().split("\\s+"));
+ long machine = java.lang.management.ManagementFactory.getOperatingSystemMXBean() instanceof com.sun.management.OperatingSystemMXBean os
+ ? os.getTotalMemorySize() : 0;
+ return javaOptions(machine / (1024 * 1024));
+ }
+
+ static List<String> javaOptions(long machineMb) {
+ long limit = machineMb <= 0 ? 2048 : Math.max(512, Math.min(4096, machineMb / 4));
+ return List.of("-XX:+UseG1GC", "-Xms64m", "-Xmx" + limit + "m", "-XX:MaxNewSize=256m",
+ "-XX:MinHeapFreeRatio=10", "-XX:MaxHeapFreeRatio=30", "-XX:G1PeriodicGCInterval=15000");
+ }
+
/** The engine's process while it is running, or -1. Started through a shell, so the engine is that shell's child. */
private long pid() {
Process p = proc;
@@ -219,8 +242,9 @@ public final class ZapEngine {
apiKey = Long.toHexString(new java.security.SecureRandom().nextLong());
Path home = this.home = workFolder();
- List<String> cmd = new ArrayList<>(List.of(
- javaBin(), "-jar", jar, "-daemon",
+ List<String> cmd = new ArrayList<>(List.of(javaBin()));
+ cmd.addAll(javaOptions());
+ cmd.addAll(List.of("-jar", jar, "-daemon",
"-host", "127.0.0.1", "-port", String.valueOf(port),
"-config", "api.key=" + apiKey,
"-config", "api.addrs.addr.name=127.0.0.1",
@@ -266,6 +290,8 @@ public final class ZapEngine {
launch.addAll(cmd);
ProcessBuilder pb = new ProcessBuilder(launch);
+ // The system's allocator keeps a pool of memory per thread unless told otherwise; the engine has many threads.
+ if (!opensuite.core.Os.WINDOWS && !opensuite.core.Os.MAC) pb.environment().putIfAbsent("MALLOC_ARENA_MAX", "2");
pb.redirectInput(ProcessBuilder.Redirect.PIPE); // the lifeline
pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
pb.redirectError(ProcessBuilder.Redirect.DISCARD);
diff --git a/tests/opensuite/engine/ScanGateTest.java b/tests/opensuite/engine/ScanGateTest.java
index 0c98f34..7096001 100644
--- a/tests/opensuite/engine/ScanGateTest.java
+++ b/tests/opensuite/engine/ScanGateTest.java
@@ -11,6 +11,14 @@ public final class ScanGateTest {
public void run() {
Harness.suite("scan gate");
+ // The engine's memory limit follows the machine, as its own launcher does, within bounds.
+ Harness.test("the engine's memory limit is a quarter of the machine, between 512 MB and 4 GB", () -> {
+ Expect.isTrue(ZapEngine.javaOptions(8192).contains("-Xmx2048m"), "8 GB machine: 2 GB");
+ Expect.isTrue(ZapEngine.javaOptions(65536).contains("-Xmx4096m"), "64 GB machine: capped at 4 GB");
+ Expect.isTrue(ZapEngine.javaOptions(1024).contains("-Xmx512m"), "1 GB machine: 512 MB");
+ Expect.isTrue(ZapEngine.javaOptions(0).contains("-Xmx2048m"), "unknown machine: 2 GB");
+ });
+
Harness.test("sites are written as host and port, the usual port filled in", () -> {
Expect.equal(ScanGate.sitesValue(List.of("http://127.0.0.1:8394", "https://app.test", "http://api.test")),
"127.0.0.1:8394 app.test:443 api.test:80", "sites");