Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large size of objects during in empty java program

Tags:

java

memory

jvm

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?


1 Answers

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

like image 95
apangin Avatar answered May 05 '26 22:05

apangin