Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6213: You can now set Ghidra MAXMEM via an assortment of environment variables

Ryan Kurtz082c04c54503
diff --git a/Ghidra/RuntimeScripts/Linux/ghidraRun b/Ghidra/RuntimeScripts/Linux/ghidraRun
index 82ebb197a6..51f8497e48 100755
--- a/Ghidra/RuntimeScripts/Linux/ghidraRun
+++ b/Ghidra/RuntimeScripts/Linux/ghidraRun
@@ -4,9 +4,16 @@
# Ghidra launch
#----------------------------------------
-# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-#MAXMEM=2G
+# Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+# Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+# specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+MAXMEM_DEFAULT=
+
+# Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+# - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+# - GHIDRA_GUI_MAXMEM: Desired maximum heap memory only for Ghidra GUI instances
+GHIDRA_MAXMEM=${GHIDRA_MAXMEM:=${MAXMEM_DEFAULT}}
+GHIDRA_GUI_MAXMEM=${GHIDRA_GUI_MAXMEM:=${GHIDRA_MAXMEM}}
# Resolve symbolic link if present and get the directory this script lives in.
# NOTE: "readlink -f" is best but works on Linux only, "readlink" will only work if your PWD
@@ -16,4 +23,4 @@ SCRIPT_FILE="$(readlink -f "$0" 2>/dev/null || readlink "$0" 2>/dev/null || echo
SCRIPT_DIR="${SCRIPT_FILE%/*}"
# Launch Ghidra
-"${SCRIPT_DIR}"/support/launch.sh bg jdk Ghidra "${MAXMEM}" "" ghidra.GhidraRun "$@"
+"${SCRIPT_DIR}"/support/launch.sh bg jdk Ghidra "${GHIDRA_GUI_MAXMEM}" "" ghidra.GhidraRun "$@"
diff --git a/Ghidra/RuntimeScripts/Linux/support/analyzeHeadless b/Ghidra/RuntimeScripts/Linux/support/analyzeHeadless
index 4e7b26fd86..369979e9cc 100755
--- a/Ghidra/RuntimeScripts/Linux/support/analyzeHeadless
+++ b/Ghidra/RuntimeScripts/Linux/support/analyzeHeadless
@@ -4,9 +4,17 @@
# Ghidra Headless Analyzer launch (see analyzeHeadlessREADME.html)
#----------------------------------------------------------------------
-# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-MAXMEM=2G
+# Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+# Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+# specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+# We override the default for headless in case users spin up many headless instances in parallel.
+MAXMEM_DEFAULT=2G
+
+# Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+# - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+# - GHIDRA_HEADLESS_MAXMEM: Desired maximum heap memory only for headless Ghidra instances
+GHIDRA_MAXMEM=${GHIDRA_MAXMEM:=${MAXMEM_DEFAULT}}
+GHIDRA_HEADLESS_MAXMEM=${GHIDRA_HEADLESS_MAXMEM:=${GHIDRA_MAXMEM}}
# Launch mode can be changed to one of the following: fg, debug, debug-suspend
LAUNCH_MODE=fg
@@ -29,4 +37,4 @@ SCRIPT_DIR="${SCRIPT_FILE%/*}"
# Launch HeadlessAnalyzer.
# DEBUG_ADDRESS set via environment for launch.sh
-DEBUG_ADDRESS=${DEBUG_ADDRESS} "${SCRIPT_DIR}"/launch.sh "${LAUNCH_MODE}" jdk Ghidra-Headless "${MAXMEM}" "${VMARG_LIST}" ghidra.app.util.headless.AnalyzeHeadless "$@"
+DEBUG_ADDRESS=${DEBUG_ADDRESS} "${SCRIPT_DIR}"/launch.sh "${LAUNCH_MODE}" jdk Ghidra-Headless "${GHIDRA_HEADLESS_MAXMEM}" "${VMARG_LIST}" ghidra.app.util.headless.AnalyzeHeadless "$@"
diff --git a/Ghidra/RuntimeScripts/Linux/support/bsim b/Ghidra/RuntimeScripts/Linux/support/bsim
index d4a1747f36..007ccb0532 100755
--- a/Ghidra/RuntimeScripts/Linux/support/bsim
+++ b/Ghidra/RuntimeScripts/Linux/support/bsim
@@ -2,9 +2,16 @@
#
# Command-line script for interacting with a BSim database
-# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-#MAXMEM=2G
+# Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+# Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+# specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+MAXMEM_DEFAULT=
+
+# Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+# - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+# - GHIDRA_BSIM_MAXMEM: Desired maximum heap memory only for Ghidra BSim instances
+GHIDRA_MAXMEM=${GHIDRA_MAXMEM:=${MAXMEM_DEFAULT}}
+GHIDRA_BSIM_MAXMEM=${GHIDRA_BSIM_MAXMEM:=${GHIDRA_MAXMEM}}
# launch mode (fg, bg, debug, debug-suspend)
LAUNCH_MODE=fg
@@ -18,4 +25,4 @@ VMARG_LIST="-Djava.awt.headless=true "
SCRIPT_FILE="$(readlink -f "$0" 2>/dev/null || readlink "$0" 2>/dev/null || echo "$0")"
SCRIPT_DIR="${SCRIPT_FILE%/*}"
-${SCRIPT_DIR}/launch.sh $LAUNCH_MODE jdk "BSim" "${MAXMEM}" "${VMARG_LIST}" ghidra.features.bsim.query.ingest.BSimLaunchable "$@"
+${SCRIPT_DIR}/launch.sh $LAUNCH_MODE jdk "BSim" "${GHIDRA_BSIM_MAXMEM}" "${VMARG_LIST}" ghidra.features.bsim.query.ingest.BSimLaunchable "$@"
diff --git a/Ghidra/RuntimeScripts/Linux/support/ghidraDebug b/Ghidra/RuntimeScripts/Linux/support/ghidraDebug
index ae96c89b02..3055f78d4f 100755
--- a/Ghidra/RuntimeScripts/Linux/support/ghidraDebug
+++ b/Ghidra/RuntimeScripts/Linux/support/ghidraDebug
@@ -4,9 +4,16 @@
# Ghidra debug launch
#----------------------------------------
-# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-#MAXMEM=2G
+# Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+# Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+# specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+MAXMEM_DEFAULT=
+
+# Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+# - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+# - GHIDRA_GUI_MAXMEM: Desired maximum heap memory only for Ghidra GUI instances
+GHIDRA_MAXMEM=${GHIDRA_MAXMEM:=${MAXMEM_DEFAULT}}
+GHIDRA_GUI_MAXMEM=${GHIDRA_GUI_MAXMEM:=${GHIDRA_MAXMEM}}
# Debug launch mode can be changed to one of the following: debug, debug-suspend
LAUNCH_MODE=debug
@@ -24,4 +31,4 @@ SCRIPT_DIR="${SCRIPT_FILE%/*}"
# Launch Ghidra in debug mode
# DEBUG_ADDRESS set via environment for launch.sh
-DEBUG_ADDRESS=${DEBUG_ADDRESS} "${SCRIPT_DIR}"/launch.sh "${LAUNCH_MODE}" jdk Ghidra "${MAXMEM}" "" ghidra.GhidraRun "$@"
+DEBUG_ADDRESS=${DEBUG_ADDRESS} "${SCRIPT_DIR}"/launch.sh "${LAUNCH_MODE}" jdk Ghidra "${GHIDRA_GUI_MAXMEM}" "" ghidra.GhidraRun "$@"
diff --git a/Ghidra/RuntimeScripts/Linux/support/jshellRun b/Ghidra/RuntimeScripts/Linux/support/jshellRun
index c9a5a1a3f8..8c661e8ad4 100755
--- a/Ghidra/RuntimeScripts/Linux/support/jshellRun
+++ b/Ghidra/RuntimeScripts/Linux/support/jshellRun
@@ -4,9 +4,16 @@
# Ghidra JShell launch
#----------------------------------------
-# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-#MAXMEM=2G
+# Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+# Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+# specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+MAXMEM_DEFAULT=
+
+# Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+# - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+# - GHIDRA_JSHELL_MAXMEM: Desired maximum heap memory only for Ghidra JShell instances
+GHIDRA_MAXMEM=${GHIDRA_MAXMEM:=${MAXMEM_DEFAULT}}
+GHIDRA_JSHELL_MAXMEM=${GHIDRA_JSHELL_MAXMEM:=${GHIDRA_MAXMEM}}
# Resolve symbolic link if present and get the directory this script lives in.
# NOTE: "readlink -f" is best but works on Linux only, "readlink" will only work if your PWD
@@ -16,4 +23,4 @@ SCRIPT_FILE="$(readlink -f "$0" 2>/dev/null || readlink "$0" 2>/dev/null || echo
SCRIPT_DIR="${SCRIPT_FILE%/*}"
# Launch Ghidra
-"${SCRIPT_DIR}"/launch.sh fg jdk Ghidra-JShell "${MAXMEM}" "" ghidra.JShellRun "$@"
+"${SCRIPT_DIR}"/launch.sh fg jdk Ghidra-JShell "${GHIDRA_JSHELL_MAXMEM}" "" ghidra.JShellRun "$@"
diff --git a/Ghidra/RuntimeScripts/Linux/support/jythonRun b/Ghidra/RuntimeScripts/Linux/support/jythonRun
index fdb0bc7a17..a3cacd5591 100755
--- a/Ghidra/RuntimeScripts/Linux/support/jythonRun
+++ b/Ghidra/RuntimeScripts/Linux/support/jythonRun
@@ -4,9 +4,16 @@
# Ghidra Jython launch
#----------------------------------------
-# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-#MAXMEM=2G
+# Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+# Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+# specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+MAXMEM_DEFAULT=
+
+# Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+# - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+# - GHIDRA_JYTHON_MAXMEM: Desired maximum heap memory only for Ghidra Jython instances
+GHIDRA_MAXMEM=${GHIDRA_MAXMEM:=${MAXMEM_DEFAULT}}
+GHIDRA_JYTHON_MAXMEM=${GHIDRA_JYTHON_MAXMEM:=${GHIDRA_MAXMEM}}
# Launch mode can be changed to one of the following: fg, debug, debug-suspend
LAUNCH_MODE=fg
@@ -33,4 +40,4 @@ SCRIPT_DIR="${SCRIPT_FILE%/*}"
# Launch Ghidra Jython
# DEBUG_ADDRESS set via environment for launch.sh
-DEBUG_ADDRESS=${DEBUG_ADDRESS} "${SCRIPT_DIR}"/launch.sh "${LAUNCH_MODE}" jdk "Ghidra-Jython" "${MAXMEM}" "${VMARG_LIST}" ghidra.jython.JythonRun "$@"
+DEBUG_ADDRESS=${DEBUG_ADDRESS} "${SCRIPT_DIR}"/launch.sh "${LAUNCH_MODE}" jdk "Ghidra-Jython" "${GHIDRA_JYTHON_MAXMEM}" "${VMARG_LIST}" ghidra.jython.JythonRun "$@"
diff --git a/Ghidra/RuntimeScripts/Windows/ghidraRun.bat b/Ghidra/RuntimeScripts/Windows/ghidraRun.bat
index a4117fa6e3..a662ef642b 100644
--- a/Ghidra/RuntimeScripts/Windows/ghidraRun.bat
+++ b/Ghidra/RuntimeScripts/Windows/ghidraRun.bat
@@ -18,9 +18,16 @@
@echo off
setlocal
-:: Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-:: the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-::set MAXMEM=2G
+:: Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+:: Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+:: specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+set MAXMEM_DEFAULT=
-call "%~dp0support\launch.bat" bg jdk Ghidra "%MAXMEM%" "" ghidra.GhidraRun %*
+:: Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+:: - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+:: - GHIDRA_GUI_MAXMEM: Desired maximum heap memory only for Ghidra GUI instances
+if not defined GHIDRA_MAXMEM set "GHIDRA_MAXMEM=%MAXMEM_DEFAULT%"
+if not defined GHIDRA_GUI_MAXMEM set "GHIDRA_GUI_MAXMEM=%GHIDRA_MAXMEM%"
+
+call "%~dp0support\launch.bat" bg jdk Ghidra "%GHIDRA_GUI_MAXMEM%" "" ghidra.GhidraRun %*
diff --git a/Ghidra/RuntimeScripts/Windows/support/analyzeHeadless.bat b/Ghidra/RuntimeScripts/Windows/support/analyzeHeadless.bat
index 496ae49803..dfdbb0ee0b 100644
--- a/Ghidra/RuntimeScripts/Windows/support/analyzeHeadless.bat
+++ b/Ghidra/RuntimeScripts/Windows/support/analyzeHeadless.bat
@@ -18,10 +18,17 @@
@echo off
setlocal
-:: Maximum heap memory size. For headless, it is recommended to not use the default value
-:: because garbage collection could take too long on systems with a large amount of physical
-:: memory.
-set MAXMEM=2G
+:: Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+:: Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+:: specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+:: We override the default for headless in case users spin up many headless instances in parallel.
+set MAXMEM_DEFAULT=2G
+
+:: Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+:: - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+:: - GHIDRA_HEADLESS_MAXMEM: Desired maximum heap memory only for headless Ghidra instances
+if not defined GHIDRA_MAXMEM set "GHIDRA_MAXMEM=%MAXMEM_DEFAULT%"
+if not defined GHIDRA_HEADLESS_MAXMEM set "GHIDRA_HEADLESS_MAXMEM=%GHIDRA_MAXMEM%"
:: Launch mode can be changed to one of the following:
:: fg, debug, debug-suspend
@@ -67,4 +74,4 @@ goto Loop
setlocal DisableDelayedExpansion
-call "%SCRIPT_DIR%launch.bat" %LAUNCH_MODE% jdk Ghidra-Headless "%MAXMEM%" "%VMARG_LIST%" ghidra.app.util.headless.AnalyzeHeadless %params%
+call "%SCRIPT_DIR%launch.bat" %LAUNCH_MODE% jdk Ghidra-Headless "%GHIDRA_HEADLESS_MAXMEM%" "%VMARG_LIST%" ghidra.app.util.headless.AnalyzeHeadless %params%
diff --git a/Ghidra/RuntimeScripts/Windows/support/bsim.bat b/Ghidra/RuntimeScripts/Windows/support/bsim.bat
index 7cad14976d..fda7fed12d 100644
--- a/Ghidra/RuntimeScripts/Windows/support/bsim.bat
+++ b/Ghidra/RuntimeScripts/Windows/support/bsim.bat
@@ -18,9 +18,16 @@
@echo off
setlocal
-:: Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-:: the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-::set MAXMEM=2G
+:: Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+:: Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+:: specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+set MAXMEM_DEFAULT=
+
+:: Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+:: - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+:: - GHIDRA_BSIM_MAXMEM: Desired maximum heap memory only for Ghidra BSim instances
+if not defined GHIDRA_MAXMEM set "GHIDRA_MAXMEM=%MAXMEM_DEFAULT%"
+if not defined GHIDRA_BSIM_MAXMEM set "GHIDRA_BSIM_MAXMEM=%GHIDRA_MAXMEM%"
:: launch mode (fg, bg, debug, debug-suspend)
set LAUNCH_MODE=fg
@@ -35,4 +42,4 @@ set LAUNCH_MODE=fg
set "LAUNCH_DIR=%~dp0"
set "LAUNCH_DIR=%LAUNCH_DIR:~0,-1%"
-call "%LAUNCH_DIR%\launch.bat" %LAUNCH_MODE% jdk BSim "%MAXMEM%" "" ghidra.features.bsim.query.ingest.BSimLaunchable %*
+call "%LAUNCH_DIR%\launch.bat" %LAUNCH_MODE% jdk BSim "%GHIDRA_BSIM_MAXMEM%" "" ghidra.features.bsim.query.ingest.BSimLaunchable %*
diff --git a/Ghidra/RuntimeScripts/Windows/support/ghidraDebug.bat b/Ghidra/RuntimeScripts/Windows/support/ghidraDebug.bat
index e5d6f1f1c0..3d5412270a 100644
--- a/Ghidra/RuntimeScripts/Windows/support/ghidraDebug.bat
+++ b/Ghidra/RuntimeScripts/Windows/support/ghidraDebug.bat
@@ -18,9 +18,16 @@
@echo off
setlocal
-:: Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-:: the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-::set MAXMEM=2G
+:: Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+:: Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+:: specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+set MAXMEM_DEFAULT=
+
+:: Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+:: - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+:: - GHIDRA_GUI_MAXMEM: Desired maximum heap memory only for Ghidra GUI instances
+if not defined GHIDRA_MAXMEM set "GHIDRA_MAXMEM=%MAXMEM_DEFAULT%"
+if not defined GHIDRA_GUI_MAXMEM set "GHIDRA_GUI_MAXMEM=%GHIDRA_MAXMEM%"
:: Debug launch mode can be changed to one of the following:
:: debug, debug-suspend
@@ -30,4 +37,4 @@ set LAUNCH_MODE=debug
:: NOTE: This variable is ignored if not launching in a debugging mode.
set DEBUG_ADDRESS=127.0.0.1:18001
-call "%~dp0launch.bat" %LAUNCH_MODE% jdk Ghidra "%MAXMEM%" "" ghidra.GhidraRun %*
+call "%~dp0launch.bat" %LAUNCH_MODE% jdk Ghidra "%GHIDRA_GUI_MAXMEM%" "" ghidra.GhidraRun %*
diff --git a/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat b/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat
index 155b46b916..0503252c75 100644
--- a/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat
+++ b/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat
@@ -18,9 +18,16 @@
@echo off
setlocal
-:: Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
-:: the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
-::set MAXMEM=2G
+:: Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+:: Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+:: specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+set MAXMEM_DEFAULT=
-call "%~dp0launch.bat" fg jdk Ghidra-JShell "%MAXMEM%" "" ghidra.JShellRun %*
+:: Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+:: - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+:: - GHIDRA_JSHELL_MAXMEM: Desired maximum heap memory only for JShell Ghidra instances
+if not defined GHIDRA_MAXMEM set "GHIDRA_MAXMEM=%MAXMEM_DEFAULT%"
+if not defined GHIDRA_JSHELL_MAXMEM set "GHIDRA_JSHELL_MAXMEM=%GHIDRA_MAXMEM%"
+
+call "%~dp0launch.bat" fg jdk Ghidra-JShell "%GHIDRA_JSHELL_MAXMEM%" "" ghidra.JShellRun %*
diff --git a/Ghidra/RuntimeScripts/Windows/support/jythonRun.bat b/Ghidra/RuntimeScripts/Windows/support/jythonRun.bat
index a4ae43bf51..1a306db339 100644
--- a/Ghidra/RuntimeScripts/Windows/support/jythonRun.bat
+++ b/Ghidra/RuntimeScripts/Windows/support/jythonRun.bat
@@ -18,12 +18,16 @@
@echo off
setlocal
-:: Maximum heap memory size
-:: Default for Windows 32-bit is 768M and 64-bit is 1024M
-:: Raising the value too high may cause a silent failure where
-:: Ghidra fails to launch.
-:: Uncomment MAXMEM setting if non-default value is needed
-::set MAXMEM=768M
+:: Optionally override the default Java heap memory, which is typically 1/4 of system RAM.
+:: Supported values are of the regular expression form "\d+[gGmMkK]", allowing the value to be
+:: specified in gigabytes, megabytes, or kilobytes (for example: 8G, 4096m, etc).
+set MAXMEM_DEFAULT=
+
+:: Allow the above MAXMEM_DEFAULT to be overridden by externally set environment variables
+:: - GHIDRA_MAXMEM: Desired maximum heap memory for all Ghidra instances
+:: - GHIDRA_JYTHON_MAXMEM: Desired maximum heap memory only for Ghidra Jython instances
+if not defined GHIDRA_MAXMEM set "GHIDRA_MAXMEM=%MAXMEM_DEFAULT%"
+if not defined GHIDRA_JYTHON_MAXMEM set "GHIDRA_JYTHON_MAXMEM=%GHIDRA_MAXMEM%"
:: Launch mode can be changed to one of the following:
:: fg, debug, debug-suspend
@@ -39,4 +43,4 @@ set DEBUG_ADDRESS=127.0.0.1:13002
set VMARG_LIST=-XX:ParallelGCThreads=2
set VMARG_LIST=%VMARG_LIST% -XX:CICompilerCount=2
-call "%~dp0launch.bat" %LAUNCH_MODE% jdk Ghidra-Jython "%MAXMEM%" "%VMARG_LIST%" ghidra.jython.JythonRun %*
+call "%~dp0launch.bat" %LAUNCH_MODE% jdk Ghidra-Jython "%GHIDRA_JYTHON_MAXMEM%" "%VMARG_LIST%" ghidra.jython.JythonRun %*
diff --git a/Ghidra/RuntimeScripts/Windows/support/sleigh.bat b/Ghidra/RuntimeScripts/Windows/support/sleigh.bat
index 15003568de..b8e27b8739 100644
--- a/Ghidra/RuntimeScripts/Windows/support/sleigh.bat
+++ b/Ghidra/RuntimeScripts/Windows/support/sleigh.bat
@@ -18,7 +18,4 @@
@echo off
setlocal
-:: maximum heap memory may be change if inadequate
-set MAXMEM=256M
-
-call "%~dp0launch.bat" fg jdk Sleigh "%MAXMEM%" "" ghidra.pcodeCPort.slgh_compile.SleighCompileLauncher %*
+call "%~dp0launch.bat" fg jdk Sleigh "" "" ghidra.pcodeCPort.slgh_compile.SleighCompileLauncher %*