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 ?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With