user7826451
user7826451

Reputation:

Large size of objects during in empty java program

I analysed simple heap dump from Java program containing only this in main method.

public static void main(String[] args) throws IOException {
    System.in.read();
}

Using HPROF parser library, my output is that:

Done, found:
    Classes: 473
    Instances: 13161
Namespace java
    Classes: 251
    Instances: 9033

How is that possible that there are so many instances without me creating any of them. Are these java runtime instances?

Upvotes: 3

Views: 103

Answers (2)

apangin
apangin

Reputation: 98630

The JVM starts execution of Java bytecode long before entering application's main method. The significant part of the JVM bootstrap procedure is written in Java. In particular, this includes:

  • Creating standard Java I/O streams;
  • Filling in system properties;
  • Bootstrapping JDK module layer;
  • Static initialization of system classes;
  • Initialization of System ClassLoader and loading the application main class.

This initialization code, having been written in Java, sometimes allocates Java objects, of course. And some of these objects stay alive for the entire lifespan of the JVM.

This answer describes an approach to find out, what these objects are, and where they are allocated.

Here is a clickable Flame Graph of objects allocated during the JVM bootstrap:

JDK bootstrap allocation Flame Graph

Upvotes: 1

Eugene
Eugene

Reputation: 121048

JVM needs a lot of classes to be loaded to run your code. One way to see them would be to use jmcd utility for this:

jcmd <YOUR_PID> VM.class_hierarchy

And you will see just how many are there. You can also do:

jcmd <YOUR_PID> GC.class_histogram

and find out how many instances of what is now in the heap and how much space it takes.

Upvotes: 0

Related Questions