Skip to content

Commit d31c843

Browse files
authored
Merge pull request #1165 from square/rick/dynamic-github-action-runner-memory
set GHA runner max heap size dynamically based upon runner hardware
2 parents a7f0ebb + ee46891 commit d31c843

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

.github/actions/gradle-args/action.yml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,44 @@ runs:
2121
run: |
2222
runnerOS=$RUNNER_OS
2323
24-
# Set common JVM arguments
25-
jvmArgs="-XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"
24+
totalMemory=0
25+
# How much memory does the OS require?
26+
memoryOverhead=0
2627
2728
case $runnerOS in
2829
macOS)
29-
jvmArgs="-Xmx10g -XX:MaxMetaspaceSize=5g $jvmArgs"
30+
totalMemory=$(sysctl -n hw.memsize | awk '{print int($1/1024/1024/1024+0.5)}')
31+
memoryOverhead=3
3032
;;
3133
Linux)
32-
jvmArgs="-Xmx4g -XX:MaxMetaspaceSize=3g $jvmArgs"
34+
totalMemory=$(awk '/MemTotal/ {print int($2/1024/1024+0.5)}' /proc/meminfo)
35+
memoryOverhead=3
3336
;;
3437
Windows)
35-
jvmArgs="-Xmx3g -XX:MaxMetaspaceSize=756m $jvmArgs"
38+
# Fetch and parse memory in MB, then convert to GB
39+
totalMemory=$(powershell -Command "& { [Math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB) }")
40+
41+
# Check if totalMemory is a valid number
42+
if ! [[ "$totalMemory" =~ ^[0-9]+$ ]]; then
43+
echo "Failed to retrieve or parse total memory: $totalMemory"
44+
exit 1
45+
fi
46+
memoryOverhead=3
3647
;;
3748
*)
3849
echo "Unsupported runner OS: $runnerOS"
3950
exit 1
4051
;;
4152
esac
4253
54+
availableMemory=$((totalMemory - memoryOverhead))
55+
56+
echo " total memory: $totalMemory"
57+
echo " memory overhead: $memoryOverhead"
58+
echo "available memory: $availableMemory"
59+
60+
jvmArgs="-Xmx${availableMemory}g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"
61+
4362
propertyArgs="-Dorg.gradle.daemon=false -Dkotlin.compiler.execution.strategy=in-process -Dkotlin.incremental=false"
4463
4564
echo "gradle-property-args=$propertyArgs" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)