Example usage for java.lang.management MemoryMXBean getHeapMemoryUsage

List of usage examples for java.lang.management MemoryMXBean getHeapMemoryUsage

Introduction

In this page you can find the example usage for java.lang.management MemoryMXBean getHeapMemoryUsage.

Prototype

public MemoryUsage getHeapMemoryUsage();

Source Link

Document

Returns the current memory usage of the heap that is used for object allocation.

Usage

From source file:com.chinamobile.bcbsp.comm.MessageQueuesForDisk.java

/**
 * Constructor.// w w  w. j ava2  s.  c  o  m
 * @param job
 * @param partitionID
 */
public MessageQueuesForDisk(BSPJob job, int partitionID) {
    LOG.info("========== Initializing Message Queues Data For Disk ==========");
    this.dataPercent = job.getMemoryDataPercent(); // Default 0.8
    this.jobID = job.getJobID();
    this.partitionID = partitionID;
    // ratio of graph data
    this.beta = job.getBeta();
    this.hashBucketNumber = job.getHashBucketNumber();
    LOG.info("[beta] = " + this.beta);
    LOG.info("[hashBucketNumber] = " + this.hashBucketNumber);
    // the structure of three kinds message queues
    this.outgoingQueues = new ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>>();
    this.incomingQueues = new ArrayList<BucketMeta>(this.hashBucketNumber);
    this.incomedQueues = new ArrayList<BucketMeta>(this.hashBucketNumber);
    for (int i = 0; i < this.hashBucketNumber; i++) {
        BucketMeta meta = new BucketMeta();
        meta.onDiskFlag = false;
        meta.length = 0;
        meta.lengthInMemory = 0;
        meta.count = 0;
        meta.countInMemory = 0;
        meta.queueMap = new ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>>();
        this.incomingQueues.add(meta);
        meta = new BucketMeta();
        meta.onDiskFlag = false;
        meta.length = 0;
        meta.lengthInMemory = 0;
        meta.count = 0;
        meta.countInMemory = 0;
        meta.queueMap = new ConcurrentHashMap<String, ConcurrentLinkedQueue<IMessage>>();
        this.incomedQueues.add(meta);
    }
    // Initialize the bucket file locks
    this.incomedFileLocks = new ReentrantLock[this.hashBucketNumber];
    this.incomingFileLocks = new ReentrantLock[this.hashBucketNumber];
    for (int i = 0; i < this.hashBucketNumber; i++) {
        this.incomedFileLocks[i] = new ReentrantLock();
        this.incomingFileLocks[i] = new ReentrantLock();
    }
    // Initialize the size of objects.
    BSPConfiguration conf = new BSPConfiguration();
    if (conf.getInt(Constants.BC_BSP_JVM_VERSION, 32) == 64) {
        sizer = ObjectSizer.forSun64BitsVM();
    } else {
        sizer = ObjectSizer.forSun32BitsVM();
    }
    this.sizeOfRef = sizer.sizeOfRef();
    this.sizeOfInteger = sizer.sizeOf(new Integer(0));
    this.sizeOfChar = sizer.sizeOfChar();
    this.sizeOfEmptyMessageQueue = sizer.sizeOf(new ConcurrentLinkedQueue<IMessage>());
    // Size will be evaluted later based on first m received messages.
    this.sizeOfMessage = MESSAGE_SIZE; // Default
    // Get the memory mxBean.
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    // Get the heap memory usage.
    MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
    long maxHeapSize = memoryUsage.getMax();
    LOG.info("[JVM max Heap size] = " + maxHeapSize / MB_SIZE + "MB");
    this.sizeOfMessagesSpace = (long) (maxHeapSize * dataPercent * (1.0f - beta));
    this.sizeThreshold = (long) (sizeOfMessagesSpace);
    this.countThreshold = (long) (sizeThreshold / sizeOfMessage);
    this.countThresholdForBucket = this.countThreshold / (3 * this.hashBucketNumber);
    LOG.info("[size of Messages Space Threshold] = " + this.sizeThreshold / MB_SIZE + "MB");
    LOG.info("[count of Messages In Memory Threshold] = " + this.countThreshold / KB_SIZE + "K");
    this.sizeOfMessagesDataInMem = 0;
    this.countOfMessagesDataInMem = 0;
    this.totalSizeOfMessages = 0;
    this.totalCount = 0;
    this.sizeOfHashMapsInMem = 0;
    this.fileRoot = new File("/tmp/bcbsp/" + this.jobID.toString() + "/" + "partition-" + this.partitionID);
    this.messagesDataFile = new File(this.fileRoot + "/" + "MessagesData");
    // If the root dir does not exit, create it.
    if (!this.fileRoot.exists()) {
        this.fileRoot.mkdirs();
    }
    // If the messages data dir does not exit, create it.
    if (!this.messagesDataFile.exists()) {
        this.messagesDataFile.mkdir();
    }
    // Initialize the current accessed bucket index.
    this.currentBucket = -1;
    LOG.info("===============================================================");
}

From source file:com.evolveum.midpoint.gui.api.util.WebComponentUtil.java

public static double getMaxRam() {
    int MB = 1024 * 1024;

    MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
    long maxHeap = mBean.getHeapMemoryUsage().getMax();
    long maxNonHeap = mBean.getNonHeapMemoryUsage().getMax();

    return (maxHeap + maxNonHeap) / MB;
}

From source file:com.evolveum.midpoint.gui.api.util.WebComponentUtil.java

public static double getRamUsage() {
    int MB = 1024 * 1024;

    MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
    long usedHead = mBean.getHeapMemoryUsage().getUsed();
    long usedNonHeap = mBean.getNonHeapMemoryUsage().getUsed();

    return (usedHead + usedNonHeap) / MB;
}

From source file:org.apache.flink.runtime.metrics.util.MetricUtils.java

private static void instantiateMemoryMetrics(MetricGroup metrics) {
    final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
    MetricGroup heap = metrics.addGroup("Heap");
    heap.gauge("Used", new Gauge<Long>() {
        @Override//from ww w  .j a va2 s  . com
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getUsed();
        }
    });
    heap.gauge("Committed", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted();
        }
    });
    heap.gauge("Max", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax();
        }
    });

    MetricGroup nonHeap = metrics.addGroup("NonHeap");
    nonHeap.gauge("Used", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    nonHeap.gauge("Committed", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    nonHeap.gauge("Max", new Gauge<Long>() {
        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getMax();
        }
    });

    List<BufferPoolMXBean> bufferMxBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);

    for (final BufferPoolMXBean bufferMxBean : bufferMxBeans) {
        MetricGroup bufferGroup = metrics.addGroup(WordUtils.capitalize(bufferMxBean.getName()));
        bufferGroup.gauge("Count", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferMxBean.getCount();
            }
        });
        bufferGroup.gauge("MemoryUsed", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferMxBean.getMemoryUsed();
            }
        });
        bufferGroup.gauge("TotalCapacity", new Gauge<Long>() {
            @Override
            public Long getValue() {
                return bufferMxBean.getTotalCapacity();
            }
        });
    }
}

From source file:org.apache.flink.runtime.taskmanager.TaskManager.java

private String getMemoryUsageStatsAsString(MemoryMXBean memoryMXBean) {
    MemoryUsage heap = memoryMXBean.getHeapMemoryUsage();
    MemoryUsage nonHeap = memoryMXBean.getNonHeapMemoryUsage();

    int mb = 20;// ww w .j  ava  2  s.co m

    long heapUsed = heap.getUsed() >> mb;
    long heapCommitted = heap.getCommitted() >> mb;
    long heapMax = heap.getMax() >> mb;

    long nonHeapUsed = nonHeap.getUsed() >> mb;
    long nonHeapCommitted = nonHeap.getCommitted() >> mb;
    long nonHeapMax = nonHeap.getMax() >> mb;

    String msg = String.format(
            "Memory usage stats: [HEAP: %d/%d/%d MB, NON HEAP: %d/%d/%d MB (used/comitted/max)]", heapUsed,
            heapCommitted, heapMax, nonHeapUsed, nonHeapCommitted, nonHeapMax);

    return msg;
}

From source file:org.apache.hadoop.gateway.websockets.WebsocketMultipleConnectionTest.java

/**
 * Test websocket proxying through gateway.
 * /*from  ww  w .  j  av a2s  . c o m*/
 * @throws Exception
 */
@Test
public void testMultipleConnections() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    final CountDownLatch latch = new CountDownLatch(MAX_CONNECTIONS);

    Session[] sessions = new Session[MAX_CONNECTIONS];

    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

    System.gc();
    final long heapt1 = memoryMXBean.getHeapMemoryUsage().getUsed();
    final long nonHeapt1 = memoryMXBean.getNonHeapMemoryUsage().getUsed();

    for (int i = 0; i < MAX_CONNECTIONS; i++) {

        sessions[i] = container.connectToServer(new WebsocketClient() {

            @Override
            public void onMessage(String message) {
                latch.countDown();

            }

        }, new URI(serverUri.toString() + "gateway/websocket/ws"));

    }

    for (int i = 0; i < MAX_CONNECTIONS; i++) {
        /* make sure the session is active and valid before trying to connect */
        if (sessions[i].isOpen() && sessions[i].getBasicRemote() != null) {
            sessions[i].getBasicRemote().sendText("OK");
        }
    }

    latch.await(5 * MAX_CONNECTIONS, TimeUnit.MILLISECONDS);

    System.gc();

    final long heapUsed = memoryMXBean.getHeapMemoryUsage().getUsed() - heapt1;
    final long nonHeapUsed = memoryMXBean.getNonHeapMemoryUsage().getUsed() - nonHeapt1;

    System.out.println("heapUsed = " + heapUsed);
    System.out.println("nonHeapUsed = " + nonHeapUsed);

    /* 90 KB per connection */
    /*
    long expected = 90 * 1024 * MAX_CONNECTIONS;
    assertThat("heap used", heapUsed, lessThan(expected));
    */
}

From source file:org.apache.hadoop.hbase.regionserver.TestCompactingMemStore.java

/**
 * Test a pathological pattern that shows why we can't currently
 * use the MSLAB for upsert workloads. This test inserts data
 * in the following pattern:// ww w .ja  v a 2  s.c  om
 *
 * - row0001 through row1000 (fills up one 2M Chunk)
 * - row0002 through row1001 (fills up another 2M chunk, leaves one reference
 *   to the first chunk
 * - row0003 through row1002 (another chunk, another dangling reference)
 *
 * This causes OOME pretty quickly if we use MSLAB for upsert
 * since each 2M chunk is held onto by a single reference.
 */
@Override
@Test
public void testUpsertMSLAB() throws Exception {

    int ROW_SIZE = 2048;
    byte[] qualifier = new byte[ROW_SIZE - 4];

    MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
    for (int i = 0; i < 3; i++) {
        System.gc();
    }
    long usageBefore = bean.getHeapMemoryUsage().getUsed();

    long size = 0;
    long ts = 0;

    for (int newValue = 0; newValue < 1000; newValue++) {
        for (int row = newValue; row < newValue + 1000; row++) {
            byte[] rowBytes = Bytes.toBytes(row);
            size += memstore.updateColumnValue(rowBytes, FAMILY, qualifier, newValue, ++ts);
        }
    }
    System.out.println("Wrote " + ts + " vals");
    for (int i = 0; i < 3; i++) {
        System.gc();
    }
    long usageAfter = bean.getHeapMemoryUsage().getUsed();
    System.out.println("Memory used: " + (usageAfter - usageBefore) + " (heapsize: " + memstore.heapSize()
            + " size: " + size + ")");
}

From source file:org.apache.hadoop.hbase.regionserver.TestDefaultMemStore.java

/**
 * Test a pathological pattern that shows why we can't currently
 * use the MSLAB for upsert workloads. This test inserts data
 * in the following pattern:/*from w  ww.  j  a  v  a  2 s. com*/
 *
 * - row0001 through row1000 (fills up one 2M Chunk)
 * - row0002 through row1001 (fills up another 2M chunk, leaves one reference
 *   to the first chunk
 * - row0003 through row1002 (another chunk, another dangling reference)
 *
 * This causes OOME pretty quickly if we use MSLAB for upsert
 * since each 2M chunk is held onto by a single reference.
 */
public void testUpsertMSLAB() throws Exception {
    Configuration conf = HBaseConfiguration.create();
    conf.setBoolean(DefaultMemStore.USEMSLAB_KEY, true);
    memstore = new DefaultMemStore(conf, KeyValue.COMPARATOR);

    int ROW_SIZE = 2048;
    byte[] qualifier = new byte[ROW_SIZE - 4];

    MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
    for (int i = 0; i < 3; i++) {
        System.gc();
    }
    long usageBefore = bean.getHeapMemoryUsage().getUsed();

    long size = 0;
    long ts = 0;

    for (int newValue = 0; newValue < 1000; newValue++) {
        for (int row = newValue; row < newValue + 1000; row++) {
            byte[] rowBytes = Bytes.toBytes(row);
            size += memstore.updateColumnValue(rowBytes, FAMILY, qualifier, newValue, ++ts);
        }
    }
    System.out.println("Wrote " + ts + " vals");
    for (int i = 0; i < 3; i++) {
        System.gc();
    }
    long usageAfter = bean.getHeapMemoryUsage().getUsed();
    System.out.println("Memory used: " + (usageAfter - usageBefore) + " (heapsize: " + memstore.heapSize()
            + " size: " + size + ")");
}

From source file:org.apache.hadoop.hbase.regionserver.TestMemStore.java

/**
 * Test a pathological pattern that shows why we can't currently
 * use the MSLAB for upsert workloads. This test inserts data
 * in the following pattern://  w  w w . j  a v  a2s . c  om
 * 
 * - row0001 through row1000 (fills up one 2M Chunk)
 * - row0002 through row1001 (fills up another 2M chunk, leaves one reference
 *   to the first chunk
 * - row0003 through row1002 (another chunk, another dangling reference)
 * 
 * This causes OOME pretty quickly if we use MSLAB for upsert
 * since each 2M chunk is held onto by a single reference.
 */
public void testUpsertMSLAB() throws Exception {
    Configuration conf = HBaseConfiguration.create();
    conf.setBoolean(MemStore.USEMSLAB_KEY, true);
    memstore = new MemStore(conf, KeyValue.COMPARATOR);

    int ROW_SIZE = 2048;
    byte[] qualifier = new byte[ROW_SIZE - 4];

    MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
    for (int i = 0; i < 3; i++) {
        System.gc();
    }
    long usageBefore = bean.getHeapMemoryUsage().getUsed();

    long size = 0;
    long ts = 0;

    for (int newValue = 0; newValue < 1000; newValue++) {
        for (int row = newValue; row < newValue + 1000; row++) {
            byte[] rowBytes = Bytes.toBytes(row);
            size += memstore.updateColumnValue(rowBytes, FAMILY, qualifier, newValue, ++ts);
        }
    }
    System.out.println("Wrote " + ts + " vals");
    for (int i = 0; i < 3; i++) {
        System.gc();
    }
    long usageAfter = bean.getHeapMemoryUsage().getUsed();
    System.out.println("Memory used: " + (usageAfter - usageBefore) + " (heapsize: " + memstore.heapSize()
            + " size: " + size + ")");
}

From source file:org.apache.hadoop.hive.ql.exec.mapjoin.MapJoinMemoryExhaustionHandler.java

private static long getMaxHeapSize(MemoryMXBean bean) {
    long maxHeapSize = bean.getHeapMemoryUsage().getMax();
    /*/*from w  ww.j  a v  a  2s. c  o  m*/
     * According to the javadoc, getMax() can return -1. In this case
     * default to 200MB. This will probably never actually happen.
     */
    if (maxHeapSize == -1) {
        LOG.warn(
                "MemoryMXBean.getHeapMemoryUsage().getMax() returned -1, " + "defaulting maxHeapSize to 200MB");
        return 200L * 1024L * 1024L;
    }
    return maxHeapSize;
}