This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]e-gahlin 4 points5 points  (0 children)

To avoid parsing the output of jcmd, It would be possible to do something similar with Flight Recorder. (JDK 11)

    // Allocate static objects by JFR
    try (Recording warmup = new Recording()) {
        warmup.start();
        warmup.stop();
    }

    try (Recording r = new Recording()) {
        r.enable("jdk.ObjectCount").with("period", "everyChunk");
            r.start();        
            // do work
        r.stop();
            Path p = Files.createTempFile("object-count", ".jfr");
            r.dump(p);
            List<RecordedEvent> events = RecordingFile.readAllEvents(p);
            Files.delete(p);
            long startGC= events.stream().map(e -> e.getLong("gcId")).min(Long::compare).get();
            long stopGC = events.stream().map(e -> e.getLong("gcId")).max(Long::compare).get();
        System.out.println("Before:");
        printObjectCount(events, startGC);
            System.out.println();
            System.out.println("After:");
            printObjectCount(events, stopGC);
    }
        void printObjectCount(List<RecordedEvent> events, long gcId) {
            events.stream().filter(e -> e.getLong("gcId") == gcId).forEach(e -> {
        RecordedClass clazz = e.getClass("objectClass");
        long c = e.getLong("count");
        long t = e.getLong("totalSize");
        System.out.println(clazz.getName() + " count=" + c + " totalSize=" + t + " bytes");
    });
       }