Java Code Examples for java.lang.management.MemoryMXBean.gc()

The following are Jave code examples for showing how to use gc() of the java.lang.management.MemoryMXBean class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: javify   File: TestMemory.java   Source Code and License Vote up 6 votes
public static void main(String[] args)
{
  MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
  System.out.println("Bean: " + bean);
  System.out.println("Heap memory usage: "
                     + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: "
                     + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: "
                     + bean.getObjectPendingFinalizationCount());
  System.out.println("Running garbage collector via bean...");
  bean.gc();
  System.out.println("Heap memory usage: "
                     + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: "
                     + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: "
                     + bean.getObjectPendingFinalizationCount());
  boolean verbosity = bean.isVerbose();
  System.out.println("Verbose memory output: " + (verbosity ? "yes" : "no"));
  System.out.println("Changing verbose setting...");
  bean.setVerbose(!verbosity);
  System.out.println("Verbose memory output: " + (bean.isVerbose() ? "yes" : "no"));
}
 
Example 2
Project: jvm-stm   File: TestMemory.java   Source Code and License Vote up 6 votes
public static void main(String[] args)
{
  MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
  System.out.println("Bean: " + bean);
  System.out.println("Heap memory usage: " 
       + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: " 
       + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: " 
       + bean.getObjectPendingFinalizationCount());
  System.out.println("Running garbage collector via bean...");
  bean.gc();
  System.out.println("Heap memory usage: " 
       + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: " 
       + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: " 
       + bean.getObjectPendingFinalizationCount());
  boolean verbosity = bean.isVerbose();
  System.out.println("Verbose memory output: " + (verbosity ? "yes" : "no"));
  System.out.println("Changing verbose setting...");
  bean.setVerbose(!verbosity);
  System.out.println("Verbose memory output: " + (bean.isVerbose() ? "yes" : "no"));
}
 
Example 3
Project: JamVM-PH   File: TestMemory.java   Source Code and License Vote up 6 votes
public static void main(String[] args)
{
  MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
  System.out.println("Bean: " + bean);
  System.out.println("Heap memory usage: " 
       + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: " 
       + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: " 
       + bean.getObjectPendingFinalizationCount());
  System.out.println("Running garbage collector via bean...");
  bean.gc();
  System.out.println("Heap memory usage: " 
       + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: " 
       + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: " 
       + bean.getObjectPendingFinalizationCount());
  boolean verbosity = bean.isVerbose();
  System.out.println("Verbose memory output: " + (verbosity ? "yes" : "no"));
  System.out.println("Changing verbose setting...");
  bean.setVerbose(!verbosity);
  System.out.println("Verbose memory output: " + (bean.isVerbose() ? "yes" : "no"));
}
 
Example 4
Project: classpath   File: TestMemory.java   Source Code and License Vote up 6 votes
public static void main(String[] args)
{
  MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
  System.out.println("Bean: " + bean);
  System.out.println("Heap memory usage: "
                     + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: "
                     + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: "
                     + bean.getObjectPendingFinalizationCount());
  System.out.println("Running garbage collector via bean...");
  bean.gc();
  System.out.println("Heap memory usage: "
                     + bean.getHeapMemoryUsage());
  System.out.println("Non-heap memory usage: "
                     + bean.getNonHeapMemoryUsage());
  System.out.println("Objects pending finalization: "
                     + bean.getObjectPendingFinalizationCount());
  boolean verbosity = bean.isVerbose();
  System.out.println("Verbose memory output: " + (verbosity ? "yes" : "no"));
  System.out.println("Changing verbose setting...");
  bean.setVerbose(!verbosity);
  System.out.println("Verbose memory output: " + (bean.isVerbose() ? "yes" : "no"));
}
 
Example 5
Project: cibet   File: MemoryMonitor.java   Source Code and License Vote up 5 votes
@Override
public MonitorResult beforeEvent(MonitorResult previousResult, LoadControlCallback callback, EventMetadata metadata,
      String currentSetpointId) {
   if (previousResult == MonitorResult.SHED || status != MonitorStatus.ON) {
      return previousResult;
   }

   MemoryPoolMXBean tenurePool = VMLoadControlJMXBean.getTenuredGenPool();
   if (tenurePool == null) {
      return previousResult;
   }

   MonitorResult result = tryShed(tenurePool, callback, metadata, currentSetpointId);
   if (result == MonitorResult.PASSED) {
      result = tryValve(tenurePool, callback, metadata, currentSetpointId);
   }
   if (result == MonitorResult.PASSED) {
      result = tryUsageAlarm(tenurePool, callback, metadata, currentSetpointId);
   }
   if (result == MonitorResult.PASSED) {
      result = tryCollectionUsageAlarm(tenurePool, callback, metadata, currentSetpointId);
   }

   if (result == MonitorResult.SHED) {
      long now = System.currentTimeMillis();
      if (garbageCollectionWaitTime > 0 && !thresholdExceededTimestamp.compareAndSet(0, now)) {
         long ts = thresholdExceededTimestamp.get();
         if (ts != 0 && now - ts > garbageCollectionWaitTime) {
            thresholdExceededTimestamp.set(0);
            MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
            memoryBean.gc();
         }
      }
   }
   return result;
}
 
Example 6
Project: javasimon   File: SimonMemoryConsumption.java   Source Code and License Vote up 5 votes
private static void report(final String what) {
	MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
	memoryMXBean.gc();
	long used = memoryMXBean.getHeapMemoryUsage().getUsed();
	long delta = used - baseline;
	System.out.println(what + ": " + delta + " (total=" + used + "), " + (delta / count) + "/unit");
}
 
Example 7
Project: javasimon   File: SimonMemoryConsumption.java   Source Code and License Vote up 5 votes
private static void setBaseLine(final String what) {
	MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
	memoryMXBean.gc();
	memoryMXBean.gc();
	baseline = memoryMXBean.getHeapMemoryUsage().getUsed();
	System.out.println(what + ": " + baseline);
}
 
Example 8
Project: appinventor-extensions   File: BuildServer.java   Source Code and License Vote up 4 votes
private static void checkMemory() {
  MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
  mBean.gc();
  LOG.info("Build " + buildCount + " current used memory: "
    + mBean.getHeapMemoryUsage().getUsed() + " bytes");
}
 
Example 9
Project: javasimon   File: SimonMemoryConsumption.java   Source Code and License Vote up 4 votes
private static void reportAbsolute(final String what) {
	MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
	memoryMXBean.gc();
	memoryMXBean.gc();
	System.out.println(what + ": " + memoryMXBean.getHeapMemoryUsage().getUsed());
}