Showing posts with label JVM. Show all posts
Showing posts with label JVM. Show all posts

Friday, July 18, 2014

What is Garbage Collection in Java ?

The Garbage collection is automatic memory management mechanism provided by java in order to reclaim memory occupied by ideal objects or objects which are no longer used in our program. Garbage collector frees programmer from manually dealing with memory de-allocation. The Garbage Collector is a demon thread which starts running automatically as soon as we execute program. So java program in execution, consists of two threads main thread and garbage collector thread. Garbage collection internally uses finalize() method of object which gives a chance to reclaim any type of resources hold by the object. We can manually call garbage collection using System.gc() but since we do not know when the object needs to be reclaimed and when the object is not in use. Manually calling System.gc() is just a hint to JVM that garbage collection is needed.

Generally Garbage collection internally runs mark and sweep algorithm which is two phase procedure.

Phase 1: Mark
The first phase in garbage collection is marking. GC will mark all the objects as alive, ideal and not referenced. It will internally create the graph of alive and ideal objects.
All the alive objects are directly reachable from the root objects. As long as object is referenced, it is marked as alive. The objects those are referenced but are not used for long time are marked as ideal and the remaining objects are marked as not referenced.

Phase 2: Sweep/Compact
In this phase, GC actually starts reclaiming the objects memory. First it will reclaim the memory of all the objects which are not referenced. Now if any class wants to create new object and if sufficient memory is not available in that case GC will look for ideal objects and will reclaim their memory. If the objects are ideal and sufficient memory is available for creating more objects then garbage collector will not reclaim ideal objects memory.

Here GC moves all the alive objects to the bottom of heap and leaves free space at the top of heap. As objects are moved, the pointers to these objects become invalid, so GC corrects these pointers and helps to solve memory fragmentation problem.

Why file name is same as class name containing main method in java ?

Whenever any member is declared as static, it can be used before any of its object is created, just by using class name. Hence static members are used just by using class name. When JVM calls main(), it does not have any instance of class having main() method. So this main() method is called as classname.main().

JVM needs classname to call main() method. How does JVM know what is your classname ?. When java program is compiled as javac filename.java, (filename is same as class name containing main() method) JVM knows your classname using which it calls main().

So file name is same as class name containing main method in java.

Thursday, July 17, 2014

Why Java Is Platform Independent

Getting output from java program is two step procedure, compilation and interpretation. Output of java compiler is not executable code but it is compiled code. JVM is an interpreter for byte code. Byte code is set of instructions which are to be executed by java run time system called as Java Virtual Machine (JVM). Each JVM has its own executable format so this byte code (.class file) becomes platform independent as this byte code runs on JVM and not on actual OS.
So once we have compiled java program, it can be executed on any platform. The details of the JVM differ from platform to platform and each OS has its own JVM format.  So in java it is “write once; run anywhere, anytime”.

So java is platform independent and JVM is highly platform dependent.

Tuesday, July 15, 2014

Why java is Secure

When it comes across the security issue of programming languages, Java is surplus over traditional programming languages like C and C++. Java is more secure than C and C++ for the following reasons:

·         Its JVM and not OS: All Java programs run over JVM platform and not on Operating system. So any OS related updates or modification does not affect execution of Java program.

·         Sandbox Security Model: Java applets are executed inside sandbox which creates an environment, which provides restricted access to OS resources and allows user to run un-trusted code from unknown source safely.

·         No Pointer Manipulation: In traditional programming memory references can be manipulated but in Java we cannot manipulate references. Thus you cannot cause an object reference to point to an arbitrary memory location, which in turn provides secure memory access.

·         Byte Code Verifier:  Byte code verifier checks correctness of class files and API libraries. Java uses this byte code verifier module which checks Java code automatically before executing it.

·         Array Boundary Check: Java provides run time array boundary check which is not provided in traditional programming.

·         Packages and Access Modifiers: This combination of packages and access modifiers allows your class to have detailed knowledge of each other, but not expose that knowledge outside that package.

·         Run Time Exception Handling: Exceptional conditions may arise in traditional programming which may lead to abnormal termination of programs but Java provides exception handling which helps to handle and eliminate this abnormal termination.

·         Dynamic Memory Allocation and De-allocation: In traditional programming C,C++ dynamic memory management is very difficult as memory allocation and de-allocation must be done by programmer but Java does this dynamic memory allocation and de-allocation without any extra efforts. Java uses new operator to dynamically allocate memory and Garbage Collection technique to handle automatic memory de-allocation.


·         Two Level of Code Checking: Code is checked two times, first at compile time and then at run time which can be considered as more secure than single level of code checking.

Why main method is public static void in java

HelloWorld.java is a sample program which contains main method.
To Compile: javac HelloWorld.java
To Execute: java HelloWorld

Lets see what is meaning of public static and void keyword in main() declaration

·         public: The main method is called by JVM when we execute “Java HelloWorld”. But since the JVM is out of scope of main method of the class HelloWorld. to make it accessible by JVM, so main method is public in java.

·         static: static keyword allows us to access members without creating object of that class with the help of class name. When JVM calls main method, it does not have any instance of the class having main method. JVM calls main method internally using class name like HelloWorld.main().So main method is static in java.

·       void: when any method does not returns any value its return type must be void. As main method does not return any value, so main method is void in java.

Saturday, July 12, 2014

Java is Compiled and Interpreted Both

It seems little confusing for java beginner to understand the exact difference between compilation and interpretation. Java is compiled language because java code is compiled to get byte code. Java is interpreted language because this byte code is interpreted into native code.

Actually getting output from java program is two steps process compilation and interpretation. The executable used for these steps are java compiler (javac) and java interpreter (java). First step is compilation in which the java compiler compiles the source code to generate byte code in the form of .class file. Actually this byte code is not machine code but it is just an intermediate code. In the second step java interpreter uses this byte code to generate native code for under lying operating system. The JIT (Just In Time) compiler comes into action in second interpretation step. The JIT reads the byte code to generate native code dynamically so that program runs fast.


Finally in short, java is compiled to byte code which is then used by JVM where it gets interpreted to native code. So java is compiled and interpreted both.

What is JVM JRE and JDK

JVM, JDK and JRE are most commonly used in java programming. Many people think that these are same or gets confused about their differences. JVM, JDK and JRE are interlinked with each other and works together in cooperative manner.
  •  JVM (Java Virtual Machine): Interpreter + JIT
JVM is code executing component of Java that executes byte code (.class file). The output of Java compilation is byte code and only JVM can understand the byte code. JVM interprets this byte code to native code depending upon underlying processor and architecture. JVM has two main components interpreter and Just In Time (JIT) compiler. JIT is compiler which helps for optimizing byte code to machine code conversion and hence results into reduced execution time.  Note that Java is platform independent but JVM is not platform independent. Each JVM has its own structure of executable which differs from platform to platform. For each OS there is different JVM available. So JVM is highly platform dependent.
  • JRE (Java Runtime Environment): JVM + byte code verifier + class loader
JRE contains an environment for execution of Java program. JRE consist of JVM, class loader and byte code verifier. Class loader module loads all the .class files (byte code) and required libraries which are then verified by byte code verifier. Then this verified code is interpreted by JVM which creates native code.
  •  JDK(Java Development Kit): JRE + Development tool
JDK contains tools like compiler and debugger to develop Java program. It provides predefined set of libraries, API classes and other files which is used by JVM at runtime. It also includes JRE, interpreter (java), compiler (javac), document generator (javadoc).