From be76ae53f9cb1f919c979c81b459378a6ae4f6be Mon Sep 17 00:00:00 2001 From: fros4943 Date: Tue, 23 Feb 2010 22:53:34 +0000 Subject: [PATCH] added warning when reloading a simulation with less then 20% memory left --- tools/cooja/java/se/sics/cooja/GUI.java | 40 ++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index d394f5eac..531b5229d 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: GUI.java,v 1.160 2010/02/21 21:51:24 fros4943 Exp $ + * $Id: GUI.java,v 1.161 2010/02/23 22:53:34 fros4943 Exp $ */ package se.sics.cooja; @@ -63,6 +63,7 @@ import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLClassLoader; import java.security.AccessControlException; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; @@ -2178,6 +2179,11 @@ public class GUI extends Observable { return; } + /* Warn about memory usage */ + if (warnMemory()) { + return; + } + /* Remove current simulation */ if (!doRemoveSimulation(true)) { return; @@ -2342,6 +2348,11 @@ public class GUI extends Observable { return; } + /* Warn about memory usage */ + if (warnMemory()) { + return; + } + final JDialog progressDialog = new JDialog(frame, "Reloading", true); final Thread loadThread = new Thread(new Runnable() { public void run() { @@ -2432,6 +2443,33 @@ public class GUI extends Observable { progressDialog.setVisible(true); } + private boolean warnMemory() { + long max = Runtime.getRuntime().maxMemory(); + long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + double memRatio = (double) used / (double) max; + if (memRatio < 0.8) { + return false; + } + + DecimalFormat format = new DecimalFormat("0.000"); + logger.warn("Reboot COOJA to avoid out of memory error! (memory usage: " + format.format(100*memRatio) + "%)"); + if (isVisualized()) { + int n = JOptionPane.showOptionDialog( + GUI.getTopParentContainer(), + "Reboot COOJA to avoid out of memory error!\n" + + "Current memory usage: " + format.format(100*memRatio) + "%.", + "Out of memory warning", + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE, null, + new String[] { "Continue", "Abort"}, "Abort"); + if (n != JOptionPane.YES_OPTION) { + return true; + } + } + + return false; + } + /** * Reload currently configured simulation. * Reloading a simulation may include recompiling Contiki.