Example usage for java.lang Runtime freeMemory

List of usage examples for java.lang Runtime freeMemory

Introduction

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

Prototype

public native long freeMemory();

Source Link

Document

Returns the amount of free memory in the Java Virtual Machine.

Usage

From source file:spade.storage.Neo4j.java

public static void index(String dbpath, boolean printProgress) {

    int totalThreads = Runtime.getRuntime().availableProcessors();
    final ConcurrentLinkedQueue<Node> nodeTaskQueue = new ConcurrentLinkedQueue<Node>();
    final ConcurrentLinkedQueue<Relationship> edgeTaskQueue = new ConcurrentLinkedQueue<Relationship>();
    final ReentrantReadWriteLock nodeRwlock = new ReentrantReadWriteLock();
    final ReentrantReadWriteLock edgeRwlock = new ReentrantReadWriteLock();
    final Index<Node> vertexIndex;
    final RelationshipIndex edgeIndex;
    System.out.println("Loading database...");
    File databaseFile = new File(dbpath);
    final GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(databaseFile)
            .setConfig(GraphDatabaseSettings.pagecache_memory,
                    "" + (Runtime.getRuntime().totalMemory() * 9) / 10)
            // .setConfig(GraphDatabaseSettings.keep_logical_logs, "false")
            .newGraphDatabase();/* w  w  w .  jav a 2 s .  co m*/

    System.out.println("Loaded");
    // clear already present indexes
    try (Transaction tx = graphDb.beginTx()) {
        graphDb.index().forNodes(spade.storage.Neo4j.VERTEX_INDEX).delete();
        tx.success();
    }

    try (Transaction tx = graphDb.beginTx()) {
        graphDb.index().forRelationships(spade.storage.Neo4j.EDGE_INDEX).delete();
        tx.success();
    }
    //

    System.out.println("Creating Indexing discriptors...");

    try (Transaction tx = graphDb.beginTx()) {
        vertexIndex = graphDb.index().forNodes(spade.storage.Neo4j.VERTEX_INDEX);
        tx.success();
    }

    try (Transaction tx = graphDb.beginTx()) {
        edgeIndex = graphDb.index().forRelationships(spade.storage.Neo4j.EDGE_INDEX);
        tx.success();
    }

    System.out.println("Created");

    class NodeIndexer implements Runnable {

        public void run() {

            Transaction tx = graphDb.beginTx();
            int counter = 0;
            try {
                while (!Thread.currentThread().isInterrupted()) {

                    if (counter < 10000) {
                        Node node = nodeTaskQueue.poll();
                        if (node == null) {
                            continue;
                        }

                        for (String key : node.getPropertyKeys()) {
                            vertexIndex.add(node, key, (String) node.getProperty(key));
                        }
                        node.setProperty(ID_STRING, node.getId());
                        vertexIndex.add(node, ID_STRING, Long.toString(node.getId()));

                        counter++;
                    }

                    if (counter > 1000 && nodeRwlock.writeLock().tryLock()) {
                        tx.success();
                        tx.close();
                        tx = graphDb.beginTx();
                        nodeRwlock.writeLock().unlock();
                        counter = 0;
                    }

                }

            } finally {
                // tx.success();
                tx.close();
                if (nodeRwlock.writeLock().isHeldByCurrentThread()) {
                    nodeRwlock.writeLock().unlock();
                }
            }
        }
    }

    class RelationshipIndexer implements Runnable {

        public void run() {

            Transaction tx = graphDb.beginTx();
            int counter = 0;
            try {
                while (!Thread.currentThread().isInterrupted()) {

                    if (counter < 10000) {
                        Relationship relationship = edgeTaskQueue.poll();
                        if (relationship == null) {
                            continue;
                        }

                        for (String key : relationship.getPropertyKeys()) {
                            edgeIndex.add(relationship, key, (String) relationship.getProperty(key));
                        }
                        relationship.setProperty(ID_STRING, relationship.getId());
                        edgeIndex.add(relationship, ID_STRING, Long.toString(relationship.getId()));

                        counter++;
                    }

                    if (counter > 1000 && edgeRwlock.writeLock().tryLock()) {
                        // tx.success();
                        tx.close();
                        tx = graphDb.beginTx();
                        edgeRwlock.writeLock().unlock();
                        counter = 0;
                    }

                }

            } finally {
                // tx.success();
                tx.close();
                if (edgeRwlock.writeLock().isHeldByCurrentThread()) {
                    edgeRwlock.writeLock().unlock();
                }
            }

        }
    }

    ArrayList<Thread> nodeWorkers = new ArrayList<Thread>();
    for (int i = 0; i < totalThreads / 2; i++) {
        Thread th = new Thread(new NodeIndexer());
        nodeWorkers.add(th);
        th.start();
    }

    ArrayList<Thread> edgeWorkers = new ArrayList<Thread>();
    for (int i = 0; i < totalThreads / 2; i++) {
        Thread th = new Thread(new RelationshipIndexer());
        edgeWorkers.add(th);
        th.start();
    }

    System.out.println("Counted Nodes and Relationships to index...");
    final long total;

    try (Transaction tx = graphDb.beginTx()) {
        total = Iterators.count(graphDb.getAllNodes().iterator())
                + Iterators.count(graphDb.getAllRelationships().iterator());
        tx.success();
    }
    System.out.println("done.\n");

    long percentageCompleted = 0;
    int count = 0;

    try (Transaction tx = graphDb.beginTx()) {

        // index nodes
        Iterator<Node> nodeIterator = graphDb.getAllNodes().iterator();
        Iterator<Relationship> edgeIterator = graphDb.getAllRelationships().iterator();

        while (edgeIterator.hasNext() || nodeIterator.hasNext()) {

            if (nodeIterator.hasNext() && nodeTaskQueue.size() < 10000) {
                nodeTaskQueue.add(nodeIterator.next());
                count = count + 1;
            }

            if (edgeIterator.hasNext() && edgeTaskQueue.size() < 10000) {
                edgeTaskQueue.add(edgeIterator.next());
                count = count + 1;
            }

            if (printProgress) {

                if (((count * 100) / total) > percentageCompleted) {
                    Runtime rt = Runtime.getRuntime();
                    long totalMemory = rt.totalMemory() / 1024 / 1024;
                    long freeMemory = rt.freeMemory() / 1024 / 1024;
                    long usedMemory = totalMemory - freeMemory;
                    System.out.print("| Cores: " + rt.availableProcessors() + " | Threads: " + totalThreads
                            + " | Heap (MB) - total: " + totalMemory + " , " + (freeMemory * 100) / totalMemory
                            + "% free"
                            // + " | Total Objects (nodes + relationships) to Index: " + total
                            + " | Indexing Object (nodes + relationships): " + count + " / " + total
                            + " | Completed: " + percentageCompleted + " %" + " |\r");
                }

                percentageCompleted = (count * 100) / total;
            }

        }

        tx.success();
    }

    System.out.println("\n\nIndexing completed. Waiting for queues to clear...");

    try {
        while (nodeTaskQueue.size() != 0 || edgeTaskQueue.size() != 0) {
            Thread.sleep(1000);
        }
    } catch (InterruptedException exception) {

    }

    System.out.println("Queues cleared. Threads teardown started...");

    for (int i = 0; i < totalThreads / 2; i++) {
        nodeWorkers.get(i).interrupt();
        try {
            nodeWorkers.get(i).join();
        } catch (InterruptedException exception) {

        }
    }

    for (int i = 0; i < totalThreads / 2; i++) {
        edgeWorkers.get(i).interrupt();
        try {
            edgeWorkers.get(i).join();
        } catch (InterruptedException exception) {

        }
    }

    System.out.println("Database shutdown started...");
    graphDb.shutdown();
}

From source file:xc.mst.utils.TimingStats.java

private void outputMemory(boolean runGC) {
    if (runGC) {//from   w ww .j  a va2 s  .c om
        System.gc();
    }
    Runtime r = Runtime.getRuntime();
    long maxMem = r.maxMemory() / 1048576;
    long totalMem = r.totalMemory() / 1048576;

    long freeBytes = r.freeMemory();
    long freeMem = freeBytes / 1048576;

    long usedMem = totalMem - freeMem;
    long memIncrease = usedMem - memUsedAtLastReset;
    memUsedAtLastReset = usedMem;

    LOG.debug("");
    LOG.debug("Free  memory: " + StringUtils.leftPad(freeMem + "", 7) + " MB.");
    LOG.debug("Used  memory: " + StringUtils.leftPad(usedMem + "", 7) + " MB.");
    LOG.debug("Increased by: " + StringUtils.leftPad(memIncrease + "", 7) + " MB.");
    LOG.debug("Total memory: " + StringUtils.leftPad(totalMem + "", 7) + " MB.");
    LOG.debug("Max'm memory: " + StringUtils.leftPad(maxMem + "", 7) + " MB.");

    //double percentageUsed = ((double) usedMem) / maxMem;
    //System.out.println("percentageUsed = " + percentageUsed);
    //MemoryWarningSystem.setPercentageUsageThreshold(0.8);  // 80%
}

From source file:org.uva.itast.TestOMRProcessor.java

@Test
public void testProcessMultiPagePDFmultiDefinitions() {
    try {//from  w  w  w . j ava 2s  .co m
        //        if (false) 
        //             return;

        URL url = getClass().getClassLoader().getResource("56605.pdf");
        File testPath = new File(url.toURI());

        // use a single, common, template definition
        prepareConfig(testPath);
        File dir = testPath.getParentFile();
        Vector<PageImage> errores;

        if (logger.isInfoEnabled()) {
            logger.info("Test with definitions in a zip."); //$NON-NLS-1$
        }

        processor.getTemplates().clear();
        File zipDefinitions = new File(dir, "form_test.zip");
        processor.loadTemplateCollection(zipDefinitions.getAbsolutePath());

        detectErrors(testPath);
        // deteccin de errores
        // use a collection of templates from a directory
        if (logger.isInfoEnabled()) {
            logger.info("Test with definitions in a directory."); //$NON-NLS-1$
        }
        processor.getTemplates().clear();
        processor.loadTemplateCollection(dir.getAbsolutePath());

        detectErrors(testPath);

    } catch (Exception e) {
        fail("Can't configure test case." + e);
    } catch (OutOfMemoryError err) {
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long maxMemory = runtime.maxMemory();
        String errorMsg = "testProcessMultiPagePDFmultiDefinitions(): Out of Memory.\n" + "Total:" + totalMemory
                + "\n" + "Max:" + maxMemory + "\n" + "Free:" + freeMemory;
        logger.fatal(errorMsg, err); //$NON-NLS-1$
        fail(errorMsg);
    }
}

From source file:er.extensions.ERXExtensions.java

/**
 * Forces the garbage collector to run. The
 * max loop parameter determines the maximum
 * number of times to run the garbage collector
 * if the memory footprint is still going down.
 * In normal cases you would just need to call
 * this method with the parameter 1. If called
 * with the parameter 0 the garbage collector
 * will continue to run until no more free memory
 * is available to collect. <br/>/*from   w w w.  j  a  v a2 s.c om*/
 * <br/>
 * Note: This can be a very costly operation and
 * should only be used in extreme circumstances.
 * @param maxLoop maximum times to run the garbage
 *      collector. Passing in 0 will cause the
 *      collector to run until all free objects
 *      have been collected.
 */
public static void forceGC(int maxLoop) {
    if (_log.isDebugEnabled())
        _log.debug("Forcing full Garbage Collection");
    Runtime runtime = Runtime.getRuntime();
    long isFree = runtime.freeMemory();
    long wasFree;
    int i = 0;
    do {
        wasFree = isFree;
        runtime.gc();
        isFree = runtime.freeMemory();
        i++;
    } while (isFree > wasFree && (maxLoop <= 0 || i < maxLoop));
    runtime.runFinalization(); //TODO: should this be inside the loop?
}

From source file:org.opensextant.examples.BasicGeoTemporalProcessing.java

public void reportMemory() {
    Runtime R = Runtime.getRuntime();
    long usedMemory = R.totalMemory() - R.freeMemory();
    log.info("CURRENT MEM USAGE(K)=" + (int) (usedMemory / 1024));
}

From source file:nz.govt.natlib.ndha.manualdeposit.dialogs.About.java

@SuppressWarnings("unchecked")
private void startup() {
    try {// w w w.j a  va  2 s  . com
        try {
            theFormControl = new FormControl(this, theSettingsPath);
        } catch (Exception ex) {
            LOG.error("Error loading form parameters", ex);
        }
        ClassLoader cLoader = Thread.currentThread().getContextClassLoader();
        Toolkit kit = Toolkit.getDefaultToolkit();
        Package p = this.getClass().getPackage();
        lblVersion.setText(p.getImplementationVersion());
        if (new File(theLogoFileName).exists()) {
            theLogo = kit.getImage(theLogoFileName);
        } else {
            java.net.URL imageURL = cLoader.getResource(theLogoFileName);
            if (imageURL != null) {
                theLogo = kit.getImage(imageURL);
            }
        }
        if (theLogo != null) {
            lblBuildNo.setForeground(Color.white);
            lblBuildNoLabel.setForeground(Color.white);
            //lblHeader.setForeground(Color.white);
            lblVersion.setForeground(Color.white);
            lblVersionLabel.setForeground(Color.white);
            paintImage();
        }
        //String manifestFileName = "/META-INF/MANIFEST.MF";
        Class cls = this.getClass();

        String className = cls.getSimpleName();
        String classFileName = className + ".class";
        String pathToThisClass = cls.getResource(classFileName).toString();
        int mark = pathToThisClass.indexOf("!");
        String pathToManifest = pathToThisClass.substring(0, mark + 1);
        if (!pathToManifest.equals("")) {
            pathToManifest += "/META-INF/MANIFEST.MF";
            LOG.debug("Path to manifest: " + pathToManifest);
            Manifest mf = new Manifest(new URL(pathToManifest).openStream());
            if (mf != null) {
                Attributes attr = mf.getMainAttributes();
                String attrVersion = "Implementation-Version";
                String attrBuild = "Implementation-Build";
                String version = attr.getValue(attrVersion);
                String build = attr.getValue(attrBuild);
                this.lblVersion.setText(version);
                this.lblBuildNo.setText(build);
            }
        }
        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long allocatedMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        LOG.debug("free memory: " + freeMemory / 1024);
        LOG.debug("allocated memory: " + allocatedMemory / 1024);
        LOG.debug("max memory: " + maxMemory / 1024);
        LOG.debug("total free memory: " + (freeMemory + (maxMemory - allocatedMemory)) / 1024);
    } catch (IOException ex) {
        ex.printStackTrace();
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:org.loklak.api.admin.StatusService.java

@Override
public JSONObject serviceImpl(Query post, HttpServletResponse response, Authorization rights,
        JSONObjectWithDefault permissions) throws APIException {

    if (post.isLocalhostAccess() && OS.canExecUnix && post.get("upgrade", "").equals("true")) {
        Caretaker.upgrade(); // it's a hack to add this here, this may disappear anytime
    }/*from   ww  w . ja v  a 2s.  c o m*/

    final String backend = DAO.getConfig("backend", "");
    final boolean backend_push = DAO.getConfig("backend.push.enabled", false);
    JSONObject backend_status = null;
    JSONObject backend_status_index_sizes = null;
    if (backend.length() > 0 && !backend_push)
        try {
            backend_status = StatusService.status(backend);
            backend_status_index_sizes = backend_status == null ? null
                    : (JSONObject) backend_status.get("index_sizes");
        } catch (IOException e) {
        }
    long backend_messages = backend_status_index_sizes == null ? 0
            : ((Number) backend_status_index_sizes.get("messages")).longValue();
    long backend_users = backend_status_index_sizes == null ? 0
            : ((Number) backend_status_index_sizes.get("users")).longValue();
    long local_messages = DAO.countLocalMessages();
    long local_users = DAO.countLocalUsers();

    post.setResponse(response, "application/javascript");

    // generate json
    Runtime runtime = Runtime.getRuntime();
    JSONObject json = new JSONObject(true);
    JSONObject system = new JSONObject(true);
    system.put("assigned_memory", runtime.maxMemory());
    system.put("used_memory", runtime.totalMemory() - runtime.freeMemory());
    system.put("available_memory", runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory());
    system.put("cores", runtime.availableProcessors());
    system.put("threads", Thread.activeCount());
    system.put("runtime", System.currentTimeMillis() - Caretaker.startupTime);
    system.put("time_to_restart", Caretaker.upgradeTime - System.currentTimeMillis());
    system.put("load_system_average", OS.getSystemLoadAverage());
    system.put("load_system_cpu", OS.getSystemCpuLoad());
    system.put("load_process_cpu", OS.getProcessCpuLoad());
    system.put("server_threads", LoklakServer.getServerThreads());
    system.put("server_uri", LoklakServer.getServerURI());

    JSONObject index = new JSONObject(true);
    long countLocalMinMessages = DAO.countLocalMessages(60000L);
    long countLocal10MMessages = DAO.countLocalMessages(600000L);
    long countLocalHourMessages = DAO.countLocalMessages(3600000L);
    long countLocalDayMessages = DAO.countLocalMessages(86400000L);
    long countLocalWeekMessages = DAO.countLocalMessages(604800000L);
    float mps1m = countLocalMinMessages / 60f;
    float mps10m = countLocal10MMessages / 600f;
    float mps1h = countLocalHourMessages / 3600f;
    float mps1d = countLocalDayMessages / 86400f;
    float mps1w = countLocalWeekMessages / 604800f;
    index.put("mps1m", mps1m);
    index.put("mps10m", mps10m);
    index.put("mps1h", mps1h);
    index.put("mps1d", mps1d);
    index.put("mps1w", mps1w);
    index.put("mps", (int) Math.max(mps1d, Math.max(mps1h, Math.max(mps10m, mps1m)))); // best of 1d, 1h and 10m
    JSONObject messages = new JSONObject(true);
    messages.put("size", local_messages + backend_messages);
    messages.put("size_local", local_messages);
    messages.put("size_local_minute", countLocalMinMessages);
    messages.put("size_local_10minutes", countLocal10MMessages);
    messages.put("size_local_hour", countLocalHourMessages);
    messages.put("size_local_day", countLocalDayMessages);
    messages.put("size_local_week", countLocalWeekMessages);
    messages.put("size_backend", backend_messages);
    messages.put("stats", DAO.messages.getStats());
    JSONObject queue = new JSONObject(true);
    queue.put("size", QueuedIndexing.getMessageQueueSize());
    queue.put("maxSize", QueuedIndexing.getMessageQueueMaxSize());
    queue.put("clients", QueuedIndexing.getMessageQueueClients());
    messages.put("queue", queue);
    JSONObject users = new JSONObject(true);
    users.put("size", local_users + backend_users);
    users.put("size_local", local_users);
    users.put("size_backend", backend_users);
    users.put("stats", DAO.users.getStats());
    JSONObject queries = new JSONObject(true);
    queries.put("size", DAO.countLocalQueries());
    queries.put("stats", DAO.queries.getStats());
    JSONObject accounts = new JSONObject(true);
    accounts.put("size", DAO.countLocalAccounts());
    JSONObject user = new JSONObject(true);
    user.put("size", DAO.user_dump.size());
    JSONObject followers = new JSONObject(true);
    followers.put("size", DAO.followers_dump.size());
    JSONObject following = new JSONObject(true);
    following.put("size", DAO.following_dump.size());
    index.put("messages", messages);
    index.put("users", users);
    index.put("queries", queries);
    index.put("accounts", accounts);
    index.put("user", user);
    index.put("followers", followers);
    index.put("following", following);
    if (DAO.getConfig("retrieval.queries.enabled", false)) {
        List<QueryEntry> queryList = DAO.SearchLocalQueries("", 1000, "retrieval_next", "date", SortOrder.ASC,
                null, new Date(), "retrieval_next");
        index.put("queries_pending", queryList.size());
    }

    JSONObject client_info = new JSONObject(true);
    client_info.put("RemoteHost", post.getClientHost());
    client_info.put("IsLocalhost", post.isLocalhostAccess() ? "true" : "false");

    JSONObject request_header = new JSONObject(true);
    Enumeration<String> he = post.getRequest().getHeaderNames();
    while (he.hasMoreElements()) {
        String h = he.nextElement();
        request_header.put(h, post.getRequest().getHeader(h));
    }
    client_info.put("request_header", request_header);

    json.put("system", system);
    json.put("index", index);
    json.put("client_info", client_info);

    return json;
}

From source file:com.ikon.servlet.admin.StatsGraphServlet.java

/**
 * Generate memory statistics/*ww w  .  j a v a2  s . co  m*/
 * http://blog.codebeach.com/2008/02/determine-available-memory-in-java.html
 */
public JFreeChart jvmMemStats() throws IOException, ServletException {
    Runtime runtime = Runtime.getRuntime();
    long max = runtime.maxMemory(); // maximum amount of memory that the JVM will attempt to use
    long available = runtime.totalMemory(); // total amount of memory in the JVM
    long free = runtime.freeMemory(); // amount of free memory in the JVM
    long used = max - available;
    long total = free + used;
    String title = "JVM memory: " + FormatUtil.formatSize(total);

    log.debug("JVM maximun memory: {}", FormatUtil.formatSize(max));
    log.debug("JVM available memory: {}", FormatUtil.formatSize(available));
    log.debug("JVM free memory: {}", FormatUtil.formatSize(free));
    log.debug("JVM used memory: {}", FormatUtil.formatSize(used));
    log.debug("JVM total memory: {}", FormatUtil.formatSize(total));

    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("Available (" + FormatUtil.formatSize(free) + ")", free * 100 / total);
    dataset.setValue("Used (" + FormatUtil.formatSize(used) + ")", used * 100 / total);

    return ChartFactory.createPieChart(title, dataset, true, false, false);
}

From source file:hydrograph.ui.joblogger.JobLogger.java

/**
 * /*from w  ww  . j a va2 s. c  o m*/
 * Logs runtime properties
 * 
 * @param jobLogger
 */
private void logRuntimeInformation(AbstractJobLogger jobLogger) {
    Runtime runtime = Runtime.getRuntime();
    long maxMemory = runtime.maxMemory();
    jobLogger.logWithNoTimeStamp("Max Memory: " + Long.toString(maxMemory / 1024));
    long allocatedMemory = runtime.totalMemory();
    jobLogger.logWithNoTimeStamp("Allocated Memory:  " + Long.toString(allocatedMemory / 1024));
    long freeMemory = runtime.freeMemory();
    jobLogger.logWithNoTimeStamp("Free Memory: " + Long.toString(freeMemory / 1024));
    jobLogger.logWithNoTimeStamp(
            "Total free memory: " + Long.toString((freeMemory + (maxMemory - allocatedMemory)) / 1024));
    long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    jobLogger.logWithNoTimeStamp("Used Memory : " + Long.toString(used));
}

From source file:at.beris.virtualfile.shell.Shell.java

private void displayStatistics() {
    Runtime runtime = Runtime.getRuntime();
    System.out.println("** Heap utilization statistics [KB] **");
    System.out/*from  ww w. jav a 2s  .c  o  m*/
            .println(String.format("Used Memory: %,d", (runtime.totalMemory() - runtime.freeMemory()) / 1024));
    System.out.println(String.format("Free Memory: %,d", runtime.freeMemory() / 1024));
    System.out.println(String.format("Total Memory: %,d", runtime.totalMemory() / 1024));
    System.out.println(String.format("Max Memory: %,d", runtime.maxMemory() / 1024));
}