Created
September 1, 2015 19:54
-
-
Save buchgr/e3884e330a49b8606b8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.sun.management.GcInfo; | |
| import javax.management.MBeanServer; | |
| import java.io.IOException; | |
| import java.lang.management.ManagementFactory; | |
| import com.sun.management.GarbageCollectorMXBean; | |
| import java.lang.management.MemoryUsage; | |
| import java.util.Map; | |
| import java.util.Random; | |
| import java.util.concurrent.Executors; | |
| import static java.util.concurrent.TimeUnit.SECONDS; | |
| class GarbageCollectorInfo { | |
| static final String YGEN_COLLECTOR_NAME = "java.lang:type=GarbageCollector,name=ParNew"; | |
| static final String OGEN_COLLECTOR_NAME = "java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"; | |
| static final GarbageCollectorMXBean ygenMBean; | |
| static final GarbageCollectorMXBean ogenMBean; | |
| static { | |
| MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | |
| GarbageCollectorMXBean mBean = null; | |
| try { | |
| mBean = ManagementFactory.newPlatformMXBeanProxy(server, YGEN_COLLECTOR_NAME, GarbageCollectorMXBean.class); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| ygenMBean = mBean; | |
| try { | |
| mBean = ManagementFactory.newPlatformMXBeanProxy(server, OGEN_COLLECTOR_NAME, GarbageCollectorMXBean.class); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| ogenMBean = mBean; | |
| } | |
| private long lastYGId; | |
| private long lastOGId; | |
| public void printYGenCollectionStats() { | |
| lastYGId = printCollectionStats(ygenMBean, lastYGId, false); | |
| } | |
| public void printOGenCollectionStats() { | |
| lastOGId = printCollectionStats(ogenMBean, lastOGId, true); | |
| } | |
| private static long printCollectionStats(GarbageCollectorMXBean mBean, Long lastId, boolean isOldGen) { | |
| GcInfo info = mBean.getLastGcInfo(); | |
| if (info == null) { | |
| return lastId; | |
| } | |
| long id = info.getId(); | |
| if (id == lastId) { | |
| return lastId; | |
| } | |
| long start = info.getStartTime(); | |
| long end = info.getEndTime(); | |
| if (start == end) { | |
| return lastId; | |
| } | |
| System.out.printf("Duration of '%s' collection %d was %d ms.\n", mBean.getName(), id, end - start); | |
| Map<String, MemoryUsage> before = info.getMemoryUsageBeforeGc(); | |
| Map<String, MemoryUsage> after = info.getMemoryUsageAfterGc(); | |
| MemoryUsage survivorBefore = before.get("Par Survivor Space"); | |
| MemoryUsage edenBefore = before.get("Par Eden Space"); | |
| MemoryUsage oldGenBefore = before.get("CMS Old Gen"); | |
| MemoryUsage survivorAfter = after.get("Par Survivor Space"); | |
| MemoryUsage edenAfter = after.get("Par Eden Space"); | |
| MemoryUsage oldGenAfter = after.get("CMS Old Gen"); | |
| long deltaSize = isOldGen ? oldGenBefore.getUsed() - oldGenAfter.getUsed() | |
| : edenBefore.getUsed() - edenAfter.getUsed(); | |
| deltaSize /= 1024 * 1024; | |
| System.out.printf("Cleaned up a total of %d MB.\n", deltaSize); | |
| return id; | |
| } | |
| } | |
| public class Main { | |
| static { | |
| Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { | |
| GarbageCollectorInfo info = new GarbageCollectorInfo(); | |
| @Override | |
| public void run() { | |
| info.printYGenCollectionStats(); | |
| info.printOGenCollectionStats(); | |
| } | |
| }, 0, 1, SECONDS); | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| byte[][] bytes = new byte[51200][]; | |
| Random rand = new Random(); | |
| for (long i = 0; i < 100000000000l; i++) { | |
| byte[] bytes0 = new byte[1024]; | |
| rand.nextBytes(bytes0); | |
| bytes[(int) (i % bytes.length)] = bytes0; | |
| } | |
| System.out.println("done" + bytes.length); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: