Java 8 MemoryMXBean - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 MemoryMXBean class.

This code example shows how to use the following methods:getHeapMemoryUsage
 
  @Override 
  public void increment() { 
    memHog.add(new byte[increment]);
 
    LOG.info("Using " + mem.getHeapMemoryUsage().getUsed() / 1024 / 1024
        + "MB of memory"); 
  } 
 
} 
6
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage, getNonHeapMemoryUsage
        reportGenerator.startGenerateReport();
 
        reportGenerator.addTestInformation();
        reportGenerator.writeWithIndent(4, "<jvm_memory_settings>");
        reportGenerator.writeWithIndent(6, "<heap_memory>");
        reportGenerator.writeWithIndent(8, "<committed>" + memoryBean.getHeapMemoryUsage().getCommitted() + "</committed>");
        reportGenerator.writeWithIndent(8, "<max>" + memoryBean.getHeapMemoryUsage().getMax() + "</max>");
        reportGenerator.writeWithIndent(6, "</heap_memory>");
        reportGenerator.writeWithIndent(6, "<non_heap_memory>");
        reportGenerator.writeWithIndent(8, "<committed>" + memoryBean.getNonHeapMemoryUsage().getCommitted() + "</committed>");
        reportGenerator.writeWithIndent(8, "<max>" + memoryBean.getNonHeapMemoryUsage().getMax() + "</max>");
        reportGenerator.writeWithIndent(6, "</non_heap_memory>");
        reportGenerator.writeWithIndent(4, "</jvm_memory_settings>");
 
        reportGenerator.addClientSettings();
        reportGenerator.endTestInformation();
    } 
 
 
    public void run() { 
5
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage
  public static long getMaxHeapSize() { 
    return getMaxHeapSize(ManagementFactory.getMemoryMXBean());
  } 
 
  private static long getMaxHeapSize(MemoryMXBean bean) {
    long maxHeapSize = bean.getHeapMemoryUsage().getMax();
    /* 
     * 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;
  } 
 
  /** 
   * Throws MapJoinMemoryExhaustionException when the JVM has consumed the 
5
CodeRank
This code example shows how to use the following methods:gc, getHeapMemoryUsage
 
    /** Creates a new instance of Pack200Test */ 
    private Pack200Test() {} 
 
    static long getUsedMemory() { 
        mmxbean.gc();
        mmxbean.gc();
        mmxbean.gc();
        return mmxbean.getHeapMemoryUsage().getUsed()/1024;
    } 
 
    private static void leakCheck() throws Exception {
        long diff = getUsedMemory() - m0;
        System.out.println("  Info: memory diff = " + diff + "K");
        if ( diff  > LEAK_TOLERANCE) {
            throw new Exception("memory leak detected " + diff);
        } 
    } 
 
    private static void doPackUnpack() { 
5
CodeRank
This code example shows how to use the following methods:isVerbose, setVerbose
     * Set verbose garbage collection logging 
     * 
     * @param verbose whether to enable verbose gc logging 
     */ 
    public static void setVerboseGarbageCollection(boolean verbose) {
        memoryMXBean.setVerbose(verbose);
        boolean checkValue = memoryMXBean.isVerbose();
        if (verbose != checkValue) {
            log.error("Could not set verbose garbage collection logging to " + verbose +
                      ", got " + checkValue + " instead");
        } 
    } 
 
    /** 
     * Initiate garbage collection via MX Bean 
     */ 
    public static void gc() { 
        memoryMXBean.gc();
    } 
 
5
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage
   * object to do so. 
   *  
   * @return the initial size in bytes 
   */ 
  public long getInitial() { 
    m_MemoryUsage = m_MemoryMXBean.getHeapMemoryUsage();
    return m_MemoryUsage.getInit();
  } 
 
  /** 
   * returns the currently used size of the JVM heap, obtains a fresh 
   * MemoryUsage object to do so. 
   *  
   * @return the used size in bytes 
   */ 
  public long getCurrent() { 
    m_MemoryUsage = m_MemoryMXBean.getHeapMemoryUsage();
    return m_MemoryUsage.getUsed();
  } 
 
3
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage
	// Start memory 
	long startMemoryMB;
	 
	@Before 
	public void before() throws Exception{
		startMemoryMB = memBean.getHeapMemoryUsage().getUsed()/MB_PER_BYTE;
	} 
	 
	@After 
	public void after(){ 
		long endMemoryMB = memBean.getHeapMemoryUsage().getUsed()/MB_PER_BYTE;
		System.out.println("Memory used = "+(endMemoryMB-startMemoryMB)+" mb");
	} 
	 
	@Test
	public void testGetSummaryNoFilter() throws Exception {
		String parentUrl = "http://synapse.sagebase.org/ontology#"+MOTILE_CELL_CONCEPT_ID;
		ConceptResponsePage response = servletTestHelper.getConceptsForParent(MOTILE_CELL_CONCEPT_ID, null, 10, 0);
		assertNotNull(response);
		assertEquals(parentUrl, response.getParentConceptUri());
4
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage
   */ 
  public HashMapWrapper(int threshold, float loadFactor, float memoryUsage) {
    maxMemoryUsage = memoryUsage;
    mHash = new HashMap<K, V>(threshold, loadFactor);
    memoryMXBean = ManagementFactory.getMemoryMXBean();
    maxMemory = memoryMXBean.getHeapMemoryUsage().getMax();
    LOG.info("maximum memory: " + maxMemory);
    num = NumberFormat.getInstance();
    num.setMinimumFractionDigits(2);
  } 
 
  public HashMapWrapper(int threshold) {
    this(threshold, LOADFACTOR, MEMORYUSAGE);
  } 
 
  public HashMapWrapper() { 
    this(THRESHOLD, LOADFACTOR, MEMORYUSAGE);
  } 
 
  public V get(K key) {
3
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage
 
    @PostConstruct 
    public void initialize() { 
        this.initializeStartTime();
        this.memoryMxBean = ManagementFactory.getMemoryMXBean();
        this.heapUsageAtStartTime = this.memoryMxBean.getHeapMemoryUsage();
 
    } 
 
    void initializeStartTime() { 
        this.startTime = ZonedDateTime.now();
    } 
 
    public ZonedDateTime getDateTime() {
        return this.startTime;
    } 
 
    public double availableMemoryInMB() { 
        MemoryUsage current = this.memoryMxBean.getHeapMemoryUsage();
        long available = (current.getCommitted() - current.getUsed());
3
CodeRank
This code example shows how to use the following methods:getHeapMemoryUsage
    /** 
     * memory that is currently bound in objects 
     * @return used bytes 
     */ 
    protected final long used() { 
        return heap.getHeapMemoryUsage().getUsed();
    } 
 
	/** 
	 * currently allocated memory in the Java virtual machine; may vary over time 
	 * @return bytes 
	 */ 
    protected final long total() { 
		return heap.getHeapMemoryUsage().getCommitted();
	} 
     
    /** 
	 * maximum memory the Java virtual will allocate machine; may vary over time in some cases 
	 * @return bytes 
	 */ 
4
CodeRank
See Code Examples for Java 8 MemoryMXBean Methods: