Skip to content

Commit

Permalink
fix(gui): limit the spare memory to max. 512MiB (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz authored and skylot committed Jan 21, 2019
1 parent aec9864 commit ffedaea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/HeapUsageBar.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jadx.gui.ui;

import jadx.gui.utils.NLS;
import jadx.gui.utils.Utils;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -44,7 +45,7 @@ public void update() {
setValue(usedKB);
setString(String.format(textFormat, (usedKB / TWO_TO_20), maxGB));

if (used > r.maxMemory() * 0.8) {
if ((used + Utils.MIN_FREE_MEMORY) > r.maxMemory()) {
setForeground(RED);
} else {
setForeground(GREEN);
Expand Down
24 changes: 22 additions & 2 deletions jadx-gui/src/main/java/jadx/gui/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public class Utils {

public static final Font FONT_HACK = openFontTTF("Hack-Regular");

/**
* The minimum about of memory in bytes we are trying to keep free, otherwise the application may run out of heap
* which ends up in a Java garbage collector running "amok" (CPU utilization 100% for each core and the UI is
* not responsive).
*
* We can calculate and store this value here as the maximum heap is fixed for each JVM instance
* and can't be changed at runtime.
*/
public static final long MIN_FREE_MEMORY = calculateMinFreeMemory();

private Utils() {
}

Expand Down Expand Up @@ -107,11 +117,21 @@ public static OverlayIcon makeIcon(AccessInfo af, Icon pub, Icon pri, Icon pro,
return overIcon;
}

/**
* @return 20% of the maximum heap size limited to 512 MB (bytes)
*/
public static long calculateMinFreeMemory() {
Runtime runtime = Runtime.getRuntime();
long minFree = (long) (runtime.maxMemory() * 0.2);
minFree = Math.min(minFree, 512 * 1048576);
return minFree;
}

public static boolean isFreeMemoryAvailable() {
Runtime runtime = Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long totalFree = runtime.freeMemory() + maxMemory - runtime.totalMemory();
return totalFree > maxMemory * 0.2;
long totalFree = runtime.freeMemory() + (maxMemory - runtime.totalMemory());
return totalFree > MIN_FREE_MEMORY;
}

public static String memoryInfo() {
Expand Down

0 comments on commit ffedaea

Please sign in to comment.