mirror of
https://github.com/correl/mage.git
synced 2024-11-15 03:00:16 +00:00
UI: added new memory statistics, warnings and hints (see #4324)
This commit is contained in:
parent
3cd7ea7752
commit
c1bf846973
3 changed files with 99 additions and 33 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Void, Float> {
|
||||
|
||||
public class UpdateMemUsageTask extends SwingWorker<Void, MemoryStats> {
|
||||
|
||||
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<Void, Float> {
|
|||
|
||||
public UpdateMemUsageTask(JLabel jLabelToDisplayInfo) {
|
||||
this.jLabelToDisplayInfo = jLabelToDisplayInfo;
|
||||
this.jLabelToDisplayInfo.setToolTipText("<html>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<Float> chunks) {
|
||||
protected void process(List<MemoryStats> 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 = "<br><br>If you see low memory warning and have free system memory then try to increase max limit in launcher settings:<br>"
|
||||
+ " - Go to <i>launcher -> settings -> java tab</i>;<br>"
|
||||
+ " - Find <i>client java options</i> (it's may contain many commands);<br>"
|
||||
+ " - Find max available memory setting: <i>-Xmx256m</i> (it's must start with <b>-Xmx</b>);<br>"
|
||||
+ " - Increase number in that value from 256 to 512, or 512 to 1024;<br>"
|
||||
+ " - Save new settings and restart application.";
|
||||
if(percent >= MEM_USAGE_WARNING_PERCENT){
|
||||
jLabelToDisplayInfo.setForeground(Color.red);
|
||||
warning = "<br><br><b>WARNING</b><br>"
|
||||
+ "Application memory limit almost reached. Errors and freezes are very possible.";
|
||||
|
||||
}else{
|
||||
jLabelToDisplayInfo.setForeground(Color.black);
|
||||
}
|
||||
|
||||
this.jLabelToDisplayInfo.setToolTipText("<html>Memory usage statistics" + warning + optimizeHint);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue