diff --git a/Mage.Client/src/main/java/mage/client/util/stats/MemoryStats.java b/Mage.Client/src/main/java/mage/client/util/stats/MemoryStats.java new file mode 100644 index 0000000000..dfa53da273 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/util/stats/MemoryStats.java @@ -0,0 +1,52 @@ +package mage.client.util.stats; + +/** + * + * @author JayDi85 + */ +public class MemoryStats { + + private float Available = 0; + private float MaxAvailable = 0; + private float Used = 0; + private float Free = 0; + + public MemoryStats(float MaxAvailable, float Available, float Used, float Free){ + this.setMaxAvailable(MaxAvailable); + this.setAvailable(Available); + this.setUsed(Used); + this.setFree(Free); + } + + public float getAvailable() { + return Available; + } + + public void setAvailable(float available) { + Available = available; + } + + public float getUsed() { + return Used; + } + + public void setUsed(float used) { + Used = used; + } + + public float getFree() { + return Free; + } + + public void setFree(float free) { + Free = free; + } + + public float getMaxAvailable() { + return MaxAvailable; + } + + public void setMaxAvailable(float maxAvailable) { + MaxAvailable = maxAvailable; + } +} \ No newline at end of file diff --git a/Mage.Client/src/main/java/mage/client/util/stats/MemoryUsageStatUtil.java b/Mage.Client/src/main/java/mage/client/util/stats/MemoryUsageStatUtil.java deleted file mode 100644 index 900b284fa9..0000000000 --- a/Mage.Client/src/main/java/mage/client/util/stats/MemoryUsageStatUtil.java +++ /dev/null @@ -1,25 +0,0 @@ -package mage.client.util.stats; - -/** - * @author noxx - */ -public final class MemoryUsageStatUtil { - - private MemoryUsageStatUtil() {} - - /** - * Returns percentage of available memory used at runtime. - * If not possible to determine, returns -1. - * - * @return - */ - public static float getMemoryFreeStatPercentage() { - Runtime runtime = Runtime.getRuntime(); - if (runtime.maxMemory() != 0) { - long usedMem = runtime.totalMemory() - runtime.freeMemory(); - return (1 - (1.0f*usedMem)/runtime.maxMemory())*100; - } else { - return -1; - } - } -} diff --git a/Mage.Client/src/main/java/mage/client/util/stats/UpdateMemUsageTask.java b/Mage.Client/src/main/java/mage/client/util/stats/UpdateMemUsageTask.java index 61b28bda4e..31d62d2496 100644 --- a/Mage.Client/src/main/java/mage/client/util/stats/UpdateMemUsageTask.java +++ b/Mage.Client/src/main/java/mage/client/util/stats/UpdateMemUsageTask.java @@ -1,5 +1,6 @@ package mage.client.util.stats; +import java.awt.*; import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; @@ -11,11 +12,13 @@ import org.apache.log4j.Logger; * This updates the mem usage info in the Mage client every * MEM_USAGE_UPDATE_TIME ms. * - * @author noxx + * @author noxx, JayDi85 */ -public class UpdateMemUsageTask extends SwingWorker { + +public class UpdateMemUsageTask extends SwingWorker { private static final int MEM_USAGE_UPDATE_TIME = 2000; + private static final int MEM_USAGE_WARNING_PERCENT = 80; // red color for mem used more than xx% private final JLabel jLabelToDisplayInfo; @@ -23,24 +26,60 @@ public class UpdateMemUsageTask extends SwingWorker { public UpdateMemUsageTask(JLabel jLabelToDisplayInfo) { this.jLabelToDisplayInfo = jLabelToDisplayInfo; + this.jLabelToDisplayInfo.setToolTipText("Memory usage statistics"); } @Override protected Void doInBackground() throws Exception { while (!isCancelled()) { - float memUsage = MemoryUsageStatUtil.getMemoryFreeStatPercentage(); - this.publish(memUsage >= 0 ? memUsage : null); + MemoryStats memoryStats = new MemoryStats(0, 0, 0, 0); + + Runtime runtime = Runtime.getRuntime(); + if (runtime.maxMemory() != 0) { + memoryStats.setMaxAvailable(runtime.maxMemory()); + memoryStats.setAvailable(runtime.totalMemory()); + memoryStats.setFree(runtime.freeMemory()); + memoryStats.setUsed(runtime.totalMemory() - runtime.freeMemory()); + } + + this.publish(memoryStats); TimeUnit.MILLISECONDS.sleep(MEM_USAGE_UPDATE_TIME); } return null; } @Override - protected void process(List chunks) { + protected void process(List chunks) { if (chunks != null && !chunks.isEmpty()) { - Float memUsage = chunks.get(chunks.size() - 1); - if (memUsage != null) { - jLabelToDisplayInfo.setText(Math.round(memUsage) + "% Mem free"); + MemoryStats memoryStats = chunks.get(chunks.size() - 1); + if (memoryStats != null) { + int max = Math.round(memoryStats.getMaxAvailable() / (1000 * 1000)); + int used = Math.round(memoryStats.getUsed() / (1000 * 1000)); + int total = Math.round(memoryStats.getAvailable() / (1000 * 1000)); + int percent = 0; + if(max != 0){ + percent = Math.round((used * 100) / max); + } + + jLabelToDisplayInfo.setText("Memory used: " + percent + "% (" + used + " of " + max + " MB)"); + String warning = ""; + String optimizeHint = "

If you see low memory warning and have free system memory then try to increase max limit in launcher settings:
" + + " - Go to launcher -> settings -> java tab;
" + + " - Find client java options (it's may contain many commands);
" + + " - Find max available memory setting: -Xmx256m (it's must start with -Xmx);
" + + " - Increase number in that value from 256 to 512, or 512 to 1024;
" + + " - Save new settings and restart application."; + if(percent >= MEM_USAGE_WARNING_PERCENT){ + jLabelToDisplayInfo.setForeground(Color.red); + warning = "

WARNING
" + + "Application memory limit almost reached. Errors and freezes are very possible."; + + }else{ + jLabelToDisplayInfo.setForeground(Color.black); + } + + this.jLabelToDisplayInfo.setToolTipText("Memory usage statistics" + warning + optimizeHint); + return; } }