Example usage for java.lang Runtime maxMemory

List of usage examples for java.lang Runtime maxMemory

Introduction

In this page you can find the example usage for java.lang Runtime maxMemory.

Prototype

public native long maxMemory();

Source Link

Document

Returns the maximum amount of memory that the Java virtual machine will attempt to use.

Usage

From source file:osmcd.program.EnvironmentSetup.java

public static void checkMemory() {
    Runtime r = Runtime.getRuntime();
    long maxHeap = r.maxMemory();
    String heapMBFormatted = String.format(Locale.ENGLISH, "%3.2f MiB", maxHeap / 1048576d);
    log.info("Total available memory to OSMCB: " + heapMBFormatted);
    if (maxHeap < 200000000) {
        String msg = String.format(I18nUtils.localizedStringForKey("msg_environment_lack_memory"),
                heapMBFormatted);//from ww  w.j a  va2s.  c  o m
        JOptionPane.showMessageDialog(null, msg,
                I18nUtils.localizedStringForKey("msg_environment_lack_memory_title"),
                JOptionPane.WARNING_MESSAGE);
    }
}

From source file:mobac.program.EnvironmentSetup.java

public static void checkMemory() {
    Runtime r = Runtime.getRuntime();
    long maxHeap = r.maxMemory();
    String heapMBFormatted = String.format(Locale.ENGLISH, "%3.2f MiB", maxHeap / 1048576d);
    log.info("Total available memory to MOBAC: " + heapMBFormatted);
    if (maxHeap < 200000000) {
        String msg = "<html><b>WARNING:</b> Mobile Atlas Creator has been started "
                + "with a very small amount of memory assigned.<br>"
                + "The current maximum usable amount of memory to Mobile Atlas Creator is <b>" + heapMBFormatted
                + "</b>.<br><br>Please make sure to start Mobile Atlas Creator in "
                + "the future via the provided start scripts <i>Mobile Atlas Creator.exe</i><br>"
                + "on Windows or <i>start.sh</i> on Linux/Unix/OSX or add the "
                + "parameter <b>-Xmx 512M</b> to your startup command.<br><br>"
                + "Example: <i>java -Xmx512M -jar Mobile_Atlas_Creator.jar</i><br>"
                + "<br><center>Press OK to continue and start Mobile Atlas Creator</center></html>";
        JOptionPane.showMessageDialog(null, msg, "Warning: low memory", JOptionPane.WARNING_MESSAGE);
    }/*from w w w  . ja  va  2 s. co  m*/
}

From source file:com.baasbox.service.storage.StatisticsService.java

public static ImmutableMap memory() {
    if (BaasBoxLogger.isTraceEnabled())
        BaasBoxLogger.trace("Method Start");
    ImmutableMap response = null;/* ww  w  .ja  va  2  s .  c  o m*/

    if (BBConfiguration.getStatisticsSystemMemory()) {
        Runtime rt = Runtime.getRuntime();
        long maxMemory = rt.maxMemory();
        long freeMemory = rt.freeMemory();
        long totalMemory = rt.totalMemory();
        response = ImmutableMap.of("max_allocable_memory", maxMemory, "current_allocate_memory", totalMemory,
                "used_memory_in_the_allocate_memory", totalMemory - freeMemory,
                "free_memory_in_the_allocated_memory", freeMemory);
    } else {
        response = ImmutableMap.of("max_allocable_memory", 0, "current_allocate_memory", 0,
                "used_memory_in_the_allocate_memory", 0, "free_memory_in_the_allocated_memory", 0);
    }

    if (BaasBoxLogger.isTraceEnabled())
        BaasBoxLogger.trace("Method End");
    return response;
}

From source file:io.digibyte.tools.util.Utils.java

@SuppressWarnings("deprecation")
public static void printPhoneSpecs() {
    String specsTag = "PHONE SPECS";
    Log.e(specsTag, "");
    Log.e(specsTag, "***************************PHONE SPECS***************************");
    Log.e(specsTag, "* screen X: " + IntroActivity.screenParametersPoint.x + " , screen Y: "
            + IntroActivity.screenParametersPoint.y);
    Log.e(specsTag, "* Build.CPU_ABI: " + Build.CPU_ABI);
    Runtime rt = Runtime.getRuntime();
    long maxMemory = rt.maxMemory();
    Log.e(specsTag, "* maxMemory:" + Long.toString(maxMemory));
    Log.e(specsTag, "----------------------------PHONE SPECS----------------------------");
    Log.e(specsTag, "");
}

From source file:com.dajodi.scandic.Util.java

public static void printMemory() {
    Runtime runtime = Runtime.getRuntime();

    long maxMemory = runtime.maxMemory();
    long allocatedMemory = runtime.totalMemory();
    long freeMemory = runtime.freeMemory();
    long totalFreeMemory = (freeMemory + (maxMemory - allocatedMemory)) / 1024;
    Log.d("free memory: " + freeMemory / 1024);
    Log.d("allocated memory: " + allocatedMemory / 1024);
    Log.d("max memory: " + maxMemory / 1024);
    Log.d("total free memory: " + totalFreeMemory);
}

From source file:com.baasbox.metrics.BaasBoxMetric.java

private static void setGauges() {
    //memory gauges
    registry.register(name(GAUGE_MEMORY_MAX_ALLOCABLE), new CachedGauge<Long>(10, TimeUnit.MINUTES) {
        @Override/* w w w  .  j  a v  a 2s  .c om*/
        public Long loadValue() {
            Runtime rt = Runtime.getRuntime();
            long maxMemory = rt.maxMemory();
            return maxMemory;
        }
    });

    registry.register(name(GAUGE_MEMORY_CURRENT_ALLOCATE), new Gauge<Long>() {
        @Override
        public Long getValue() {
            Runtime rt = Runtime.getRuntime();
            long totalMemory = rt.totalMemory();
            return totalMemory;
        }
    });

    registry.register(name(GAUGE_MEMORY_USED), new Gauge<Long>() {
        @Override
        public Long getValue() {
            Runtime rt = Runtime.getRuntime();
            long freeMemory = rt.freeMemory();
            long totalMemory = rt.totalMemory();
            return totalMemory - freeMemory;
        }
    });
    registry.register(name(GAUGE_FILESYSTEM_DATAFILE_SPACE_LEFT),
            new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public Long loadValue() {
                    return new File(BBConfiguration.getDBDir()).getFreeSpace();
                }
            });

    registry.register(name(GAUGE_FILESYSTEM_BACKUPDIR_SPACE_LEFT),
            new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public Long loadValue() {
                    return new File(BBConfiguration.getDBBackupDir()).getFreeSpace();
                }
            });

    registry.register(name(GAUGE_DB_DATA_SIZE), new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
        @Override
        public Long loadValue() {
            boolean opened = false;
            try {
                if (DbHelper.getConnection() == null || DbHelper.getConnection().isClosed()) {
                    DbHelper.open(BBConfiguration.getAPPCODE(), BBConfiguration.getBaasBoxAdminUsername(),
                            BBConfiguration.getBaasBoxAdminUsername());
                    opened = true;
                }
                return DbHelper.getConnection().getSize();
            } catch (InvalidAppCodeException e) {
                throw new RuntimeException(e);
            } finally {
                if (opened)
                    DbHelper.close(DbHelper.getConnection());
            }
        }
    });

    registry.register(name(GAUGE_DB_DATA_DIRECTORY_SIZE),
            new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public Long loadValue() {
                    return FileUtils.sizeOfDirectory(new File(BBConfiguration.getDBDir()));
                }
            });
    registry.register(name(GAUGE_DB_MAX_SIZE_THRESHOLD),
            new CachedGauge<BigInteger>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public BigInteger loadValue() {
                    return BBConfiguration.getDBSizeThreshold();
                }
            });

}

From source file:com.nridge.core.base.std.Platform.java

/**
 * Create a log message containing JVM Heap Memory statistics.
 * <p>totalMemory(): Returns the total amount of memory in the
 * Java virtual machine. The value returned by this method may
 * vary over time, depending on the host environment. Note that
 * the amount of memory required to hold an object of any given
 * type may be implementation-dependent.</p>
 * <p>maxMemory(): Returns the maximum amount of memory that the
 * Java virtual machine will attempt to use. If there is no inherent
 * limit then the value Long.MAX_VALUE will be returned.</p>
 * <p>freeMemory(): Returns the amount of free memory in the Java
 * Virtual Machine. Calling the gc method may result in increasing
 * the value returned by freeMemory.</p>
 * <p>In reference to your question, maxMemory() returns the -Xmx value.
 * You may be wondering why there is a totalMemory() AND a maxMemory().
 * The answer is that the JVM allocates memory lazily.</p>
 *
 * @param aTitle Title to save with log entry.
 *
 * @return Log message.// w  ww.  ja  v  a 2  s  .  c  o  m
 *
 * @see <a href="http://stackoverflow.com/questions/3571203/what-is-the-exact-meaning-of-runtime-getruntime-totalmemory-and-freememory">Runtime Memory</a>
 * @see <a href="http://www.mkyong.com/java/find-out-your-java-heap-memory-size/">Heap Memory</a>
 * @see <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html">JavaDoc Runtime</a>
 */
public static String jvmLogMessage(String aTitle) {
    Runtime jvmRuntime = Runtime.getRuntime();

    if (StringUtils.isEmpty(aTitle))
        aTitle = "JVM";

    long maxMemory = jvmRuntime.maxMemory();
    long freeMemory = jvmRuntime.freeMemory();
    long totalMemory = jvmRuntime.totalMemory();
    long usedMemory = totalMemory - freeMemory;
    long availMemory = maxMemory - usedMemory;

    String logMsg = String.format("%s: Processors: %d, Mem Max: %s, Mem Total: %s, Mem Used: %s, Mem Avail: %s",
            aTitle, jvmRuntime.availableProcessors(), bytesToString(maxMemory), bytesToString(totalMemory),
            bytesToString(usedMemory), bytesToString(availMemory));
    return logMsg;
}

From source file:org.apache.flink.runtime.instance.HardwareDescriptionFactory.java

/**
 * Returns the size of free memory in bytes available to the JVM.
 * /*from w  w w .  j a v  a 2s .  co  m*/
 * @return the size of the free memory in bytes available to the JVM or <code>-1</code> if the size cannot be
 *         determined
 */
private static long getSizeOfFreeMemory() {
    Runtime r = Runtime.getRuntime();
    long available = r.maxMemory() - r.totalMemory() + r.freeMemory();

    return available;
}

From source file:net.nosleep.superanalyzer.util.Misc.java

public static void printMemoryInfo(String tag) {
    Runtime rt = Runtime.getRuntime();
    long free = rt.freeMemory();
    long total = rt.totalMemory();
    long max = rt.maxMemory();

    System.out.println("Memory (" + tag + "): " + free + "/" + total + " (max " + max + ")");
}

From source file:com.bstek.dorado.console.utils.SystemUtils.java

/**
 * ??/*from w  w  w  .  jav  a2s.c o  m*/
 * 
 * @return
 */
public static Map<String, Object> getMemoryInfo() {
    Map<String, Object> map = new HashMap<String, Object>();
    Runtime runtime = Runtime.getRuntime();
    map.put("runtime", runtime);

    map.put("freeMemory", runtime.freeMemory());
    map.put("totalMemory", runtime.totalMemory());
    if (System.getProperty("java.version").compareTo("1.4") >= 0)
        map.put("maxMemory", runtime.maxMemory());
    else
        map.put("maxMemory", "N/A");

    map.put("CPU", runtime.availableProcessors());
    return map;
}