1

Is there a way to get the java object creation rate programmatically? Or, the total size of object created so far, not considering the garbage collection.

Such as

int size = GC.getSizeTotalObjectCreated();

// do some object creation

int size2 = GC.getSizeTotalObjectCreated();

System.out.println("TotalObjectCreated" + (size2 - size));
jaeyong
  • 8,951
  • 14
  • 50
  • 63
  • [`MemoryMXBean`](https://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html), see [also](http://stackoverflow.com/questions/32398555/how-to-monitor-memory-after-major-garbage-collection-via-jmx-or-code). – Elliott Frisch Apr 27 '17 at 16:22
  • 1
    Do you want the rate or do you want to measure the size? These are two different things. Rate deals w/time. I don't think that's what you're asking. – Rabbit Guy Apr 27 '17 at 16:23
  • I meant rate. But the size is equally ok as dealing with time wouldn't be so hard. – jaeyong Apr 28 '17 at 23:24

1 Answers1

0

Using MemoryPoolMXBean heap size can be found along with the usage details of Eden/Survivor/Old Gen.

Object creation rate can be calculated on the basis of free heap size difference at given two points in time.

public void printHeapDetails() {        
    System.out.println("Heap size: " + Runtime.getRuntime().totalMemory());
    System.out.println("Free heap size: "
            + Runtime.getRuntime().freeMemory());

    for (MemoryPoolMXBean bean : ManagementFactory.getMemoryPoolMXBeans()) {
        if (bean.getType() == MemoryType.HEAP)
            System.out.printf("Name: %s: %s\n", bean.getName(),
                    bean.getUsage());
    }
}
Pardeep
  • 945
  • 10
  • 18
  • Thanks for your answer. But it is not really I am looking for. If garbage collection occurring in the middle of measurement it will give wrong value. – jaeyong Apr 28 '17 at 23:25