Skip to content

Commit 9d6abfd

Browse files
author
pepc84
committed
Fix OOM: skip PCH on low memory, cap linter output, run bestCppStd off EDT
1 parent d35f405 commit 9d6abfd

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

mode/CppMode.jar

737 Bytes
Binary file not shown.

src/java/CppBuild.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,21 @@ private List<String> buildCommand(String gpp, File src, File bin, boolean win, b
31133113
// Declared outside try so they are visible after the catch block.
31143114
File processingPch = null;
31153115
boolean needsPch = true;
3116+
// Skip PCH on low-memory machines (< 4GB free) to avoid OOM
3117+
long freeMemBytes = Runtime.getRuntime().maxMemory() -
3118+
(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
3119+
boolean lowMemory = freeMemBytes < 512L * 1024 * 1024; // < 512MB JVM headroom
3120+
// Also check system RAM
3121+
try {
3122+
java.lang.management.OperatingSystemMXBean osmx =
3123+
(java.lang.management.OperatingSystemMXBean)
3124+
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
3125+
if (osmx instanceof com.sun.management.OperatingSystemMXBean sunOs) {
3126+
long freeRam = sunOs.getFreeMemorySize();
3127+
if (freeRam < 2L * 1024 * 1024 * 1024) lowMemory = true; // < 2GB free RAM
3128+
}
3129+
} catch (Exception ignored) {}
3130+
if (lowMemory) needsPch = false;
31163131
try {
31173132
String osName = System.getProperty("os.name","").toLowerCase();
31183133
String cacheSubDir = osName.contains("win") ? "cache/windows-x64"

src/java/CppEditor.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,21 @@ private void addStdDropdown(EditorFooter ef) {
7373

7474
Preferences prefs = Preferences.userNodeForPackage(CppEditor.class);
7575
String gpp = "g++";
76-
String bestStd = processing.mode.cpp.CppBuild.bestCppStd(gpp);
76+
// Run bestCppStd off the EDT to avoid blocking the UI
77+
String bestStd = prefs.get(PREF_KEY + ".cached_best", "c++17");
78+
new Thread(() -> {
79+
String detected = processing.mode.cpp.CppBuild.bestCppStd(gpp);
80+
prefs.put(PREF_KEY + ".cached_best", detected);
81+
javax.swing.SwingUtilities.invokeLater(() -> {
82+
if (stdCombo == null) return;
83+
// Update combo with detected standard
84+
int detectedIdx = 0;
85+
for (int i = 0; i < CPP_STDS.length; i++)
86+
if (CPP_STDS[i].equals(detected)) { detectedIdx = i; break; }
87+
// Re-enable/disable items based on detected support
88+
stdCombo.repaint();
89+
});
90+
}, "CppStdProbe").start();
7791
String savedStd = prefs.get(PREF_KEY, bestStd);
7892

7993
int bestIdx = 0;

src/java/CppLinter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ private void runCheck(Sketch sketch, int currentTab, String liveText) {
186186
ProcessBuilder pb = new ProcessBuilder(cmd);
187187
pb.redirectErrorStream(true);
188188
Process proc = pb.start();
189-
String output = new String(proc.getInputStream().readAllBytes());
189+
// Cap output to 64KB to avoid OOM on large error outputs
190+
byte[] rawOut = proc.getInputStream().readNBytes(64 * 1024);
191+
String output = new String(rawOut);
190192
proc.waitFor(20, TimeUnit.SECONDS);
191193

192194
// Parse errors and map back to tab + line

0 commit comments

Comments
 (0)