Friday, September 19, 2008

Hello World Classes

All Programas writted in Java need to be defined in a Class, this class depend on other classes and those depend on others as well, so when we run an application the JVM needs to load those classes to run the program.

So how can we know the classes that the JVM loads?
The answer is simple:
  We have to use the -verbose:class option of the JVM.



The traditional exercise to start learning a programming is the "Hello World" application.
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");
}
}

This is a very simple class in Java and seems to need few classes. Of course java.lang.Object always be loaded is the father of all clases, java.lang.String class, and some PrintStreams for writting the String in the console.

Lets check the real loaded classes:
 > java -verbose:class HelloWorld

Woow! surprise!
I ran this program with Java 1.6 u5 and the result was: 314 classes loaded.

This classes are loaded in a special part of the Java Heap called Perm Generation. 
We have to be aware of how many classes are loaded and the memory they consume and maybe do some adjustments to minimize the loaded classes.

0 comments: