A java trainer who trained in our company claims there are 380 objects created to run the simplest "Hello World" program. Is this correct ? How do I verify it ?
-
i would look into a new trainer – Frank Visaggio Jul 24 '12 at 08:41
-
2@BobSinclar Why? He is likely right (the Trainer). Is "being correct" something you do not like? – TomTom Jul 24 '12 at 08:43
-
1I'm not sure what knowing this would accomplish or why you would mention it. Also giving a concrete number seems silly. – Frank Visaggio Jul 24 '12 at 08:49
-
If it seems silly and unreasonable, just click Back and find another question. These comments are really pointless. – Marko Topolnik Jul 24 '12 at 08:51
-
is it not correct, actually its loading much more than that. and more than 1300 classes. – kdureidy Jun 19 '13 at 14:16
3 Answers
Is this correct ?
Very likely. THere is a lot of infrastructure that Java starts every time and / or that just has to be there.
How do I verify it ?
Memory Profiler.
not that it matters - most will be quite small or baseline infrastructure elements. What you found out now is that Java is not very slim doing nothing. Who cares? Most Java programs are not that simple.

- 61,059
- 10
- 88
- 148
Edit
It might well be, to get an idea on what is done by the JVM you can take a look at the number of classes are loaded to execute a simple program. These will be the loaded classes and not the instances (for this you will need to profile your program) but it's a quick way to get a general idea.
Write a simple HelloWorld class (even with an empty main method)
public class HelloWorld {
public static void main(String [] arguments) {
}
}
Compile it and execute it with the -verbose
option. The JVM will tell you which classes are loaded
$ javac HelloWorld.java
$ java -verbose HelloWorld
[Opened /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.Object from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.io.Serializable from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.Comparable from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.CharSequence from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.String from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
you can count the lines with grep
and wc
if you are on a Unix environment
$ java -verbose HelloWorld | grep Loaded | wc -l
588

- 14,696
- 9
- 68
- 106
-
Good point, this gives the **lower bound** on object count, and it's already well above 380! Except if this counts only class **loading** and not class **initialization** -- before initialization no objects are created. – Marko Topolnik Jul 24 '12 at 08:47
-
@MarkoTopolnik Yes but it gives the OP an idea on the number of needed classes. This is a quick way to see what the teacher meant. – Matteo Jul 24 '12 at 08:59
-
1Just be careful not to confuse class loading with class instantiation. Class loading can happen at any time and for any reason an implementation sees fit. By itself it makes no contribution to the number of created objects. – Marko Topolnik Jul 24 '12 at 09:02
-
@MarkoTopolnik You are right better clarify it (I edited the answer). I just wanted to give the OP a quick way to grasp the complexity. A precise analysis will of course need a profiler (this is not even the lower bound as some classes could be loaded but not instantiated, for example all the interfaces are marked as loaded classes) – Matteo Jul 24 '12 at 09:08
-
1I took that into account---when a class is initialized, at the very least its `Class` object is created---that's the one instance I had in mind. – Marko Topolnik Jul 24 '12 at 09:10
-
The initialization code in the classes from the standard library creates objects. You can verify this using VisualVM, for example.
What was not left clear is whether this is the total objects created, or the total objects retained (not eligible for GC), and this is a very important difference. If this is the total number of objects created, then I can only say this is a sign of truly well-optimized initialization procedure. 380 short-lived object allocations is virtually nothing.

- 195,646
- 29
- 319
- 436