Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My trainer says Java creates 380 objects to run a simple java program.Is this right? [closed]

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 ?

like image 807
user2434 Avatar asked Dec 13 '22 00:12

user2434


2 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.

like image 123
TomTom Avatar answered Apr 20 '23 00:04

TomTom


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
like image 43
Matteo Avatar answered Apr 20 '23 01:04

Matteo