Saturday, 14 June 2014

Java Question and Answers on JVM and Garbage Collection

Posted by Charan Veera
Hey java is a modern object oriented language, which has been designed keeping the Internet in mind. java has eliminated many problematic areas found in C++. This covers the Java interview questions on JVM and Garbage Collection.
 

Java interview questions :

How many JVMs can run on a single machine?

No limitation.A JVM is just like any other process.

What is the Just-In-Time Compiler?

The Java interpreter on any platform will interpret the compiled bytecode into instructions understandable by the particular hardware. However, the Java Virtual Machine handles one bytecode instruction at a time. The Java Just-In-Time Compiler compiles the bytecode into the particular machine code. Once the code has been compiled by the JIT compiler, it will usually run more quickly in that system.

What is the use of 'classpath' environment variable ?

Classpath is the list of directories through which the JVM will search to find a class. It is different from the OS environment variable PATH.
Note: Classpath that a JVM has to use can be specified using the classpath option as follows:If this is not specified then the value specified for the
environment is taken.
C:>java -classpath classpath1;classpath2 MyClass.java

Can Java code be retrieved from byte-code?

Yes, class files can be decompiled using utilities like JAD. This takes the class file as input and generates a Java file. this is especially useful when you are confused a to  which version of Java file was used to generate the class file, in a multi-user environment.

What is a Garbage Collector?

When an object is no longer required, its memory needs to be cleaned up an freed. This is what the garbage collector does. Garbage Collector is a thread running as a part of the JVM process. This thread scans program for objects that will never be accessed again and release their resources back to the system. The basic garbage collector uses mark and sweep algorithm. This algorithm marks all the unused variables and sweep them.

Can the Garbage Collector be forced to run?

No.Calling the System.gc( ) method only suggests that the JVM expend effort toward recycling unused objects in order to make the memory they currently occupy available for reuse. When control returns from the method call,  the JVM has made a best effort to reclaim space from all discarded objects. This does not guarantee that the garbage collector is called.
The call System.gc( ) is effectively equivalent to the call : Runtime.getRuntime( ).gc( )

0 comments:

Post a Comment