Skip to content

Latest commit

 

History

History
167 lines (101 loc) · 12.7 KB

File metadata and controls

167 lines (101 loc) · 12.7 KB

Java Virtual Machine

List questions:

  1. What is a Java Virtual Machine (JVM)?
  2. Memory Management with JVM?
  3. Explain the Difference amongst JVM Specification, JVM Implementation, JVM Runtime?
  4. What is the size of int in 64-bit JVM?
  5. Why is the source file named after the class?
  6. Which class should be compiled first?
  7. What are the steps in compiling a class?
  8. The difference between Serial and Parallel Garbage Collector?
  9. A difference between WeakReference and SoftReference in Java?
  10. How do WeakHashMap works?
  11. What is -XX:+UseCompressedOops JVM option? Why use it?
  12. How do you find if JVM is 32-bit or 64-bit from Java Program?
  13. What is the maximum heap size of 32-bit and 64-bit JVM?
  14. What is the difference between JRE, JDK, JVM and JIT?
  15. Explain Java Heap space and Garbage collection?
  16. How do you find memory usage from Java program? How much percent of the heap is used
  17. What is the difference between stack and heap in Java?
  18. Oracle JDK vs OpenJDK

  1. What is a Java Virtual Machine (JVM)?

Java virtual machine is a runtime environment required for execution of a Java application.

Every Java application runs inside a runtime instance of some concrete implementation of abstract specifications of JVM.

JVM is crux of "platform independent" nature of the language.

JVM translates bytecode into machine language

Every Java program is first compiled into an intermediate language called Java bytecode. The JVM is used primarily for 2 things:

  • The first is to translate the bytecode into the machine language for a particular computer, and the second thing is to actually execute the corresponding machine-language instructions as well.
  • The JVM and bytecode combined give Java its status as a "portable" language – this is because Java bytecode can be transferred from one machine to another.

Machine language is OS dependent

The JVM is not platform independent

Reference: programmerinterview , interviewjava, roseindia , artima

  1. Memory Management with JVM?

The Java language in combination with runtime eliminates the problems of memory management and corrupted pointers.

  1. There is no explicit allocation of memory in Java, memory is allocated only to objects.

  2. JVM's heap stores all objects created.

  3. JVM ask the operating system for enough memory to run the JVM itself and some free memory for the application to create new objects.

  4. If the free memory area is getting too small, the JVM will ask the operating system for more and if there is no more additional memory available from the operating system, then JVM stops the application and issues the "Out of memory error".

  5. The Java runtime employs a garbage collector to reclaim the memory occupied by an object.

  6. Explain the Difference amongst JVM Specification, JVM Implementation, JVM Runtime?

JVM Specification = Java programming language + Java Virtual Machine architecture + Java class-file format.

  1. What is the size of int in 64-bit JVM?

The size of an int variable is constant in Java, it's always 32-bit irrespective of platform. Which means the size of primitive int is same in both 32-bit and 64-bit Java virtual machine.

It's always 32 bits or 4 bytes.

  1. Why is the source file named after the class?

The Java source file naming convention is not a standard specified by the Java language but is a common feature of Java compilers, such as javac, to help locate source code. The source content of a Java class is known as a compilation unit. By storing the compilation unit in a file that is named after the class, the compiler can locate any supporting classes by name and compile those too.

This convention also extends to package names. Most Java compilers expect source code to be stored in directories whose names match their package hierarchy. Thus the source code for a class named Example in the package com.domain.util might be stored in a file with the path c:\src\com\domain\util\Example.java

Reference: codeproject , javabeat

  1. Which class should be compiled first?

Sometimes you will find that trial and error will give you the answer you need. If you are using the Sun compiler, javac, the compiler will compile any other classes that your target class depends on, provided the other source files are in the same directory hierarchy as the first and the sub-directory names reflect the package hierarchy of the classes.

  1. What are the steps in compiling a class?
  2. The difference between Serial and Parallel Garbage Collector?

Even though both the serial and parallel collectors cause a stop-the-world pause during Garbage collection. The main difference between them is that a serial collector is a default copying collector which uses only one GC thread for garbage collection while a parallel collector uses multiple GC threads for garbage collection.

detail

  1. A difference between WeakReference and SoftReference in Java?

Though both WeakReference and SoftReference helps garbage collector and memory efficient, WeakReference becomes eligible for garbage collection as soon as last strong reference is lost but SoftReference even thought it can not prevent GC, it can delay it until JVM absolutely need memory.

detail

  1. How do WeakHashMap works?

WeakHashMap works like a normal HashMap but uses WeakReference for keys, which means if the key object doesn't have any reference then both key/value mapping will become eligible for garbage collection.

  1. What is -XX:+UseCompressedOops JVM option? Why use it?

When you go migrate your Java application from 32-bit to 64-bit JVM, the heap requirement suddenly increases, almost double, due to increasing size of ordinary object pointer from 32 bit to 64 bit. This also adversely affect how much data you can keep in CPU cache, which is much smaller than memory. Since main motivation for moving to 64-bit JVM is to specify large heap size, you can save some memory by using compressed OOP. By using -XX:+UseCompressedOops, JVM uses 32-bit OOP instead of 64-bit OOP.

detail

  1. How do you find if JVM is 32-bit or 64-bit from Java Program?

You can find that by checking some system properties like sun.arch.data.model or os.arch

detail

  1. What is the maximum heap size of 32-bit and 64-bit JVM?

Theoretically, the maximum heap memory you can assign to a 32-bit JVM is 2^32 which is 4GB but practically the limit is much smaller. It also varies between operating systems e.g. form 1.5GB in Windows to almost 3GB in Solaris. 64-bit JVM allows you to specify larger heap size, theoretically 2^64 which is quite large but practically you can specify heap space up to 100GBs. There are even JVM e.g. Azul where heap space of 1000 gigs is also possible.

detail

  1. What is the difference between JRE, JDK, JVM and JIT?
  • JRE stands for Java run-time and it's required to run Java application.

  • JDK stands for Java Development Kit and provides tools to develop Java program e.g. Java compiler. It also contains JRE.

  • The JVM stands for Java virtual machine and it's the process responsible for running Java application.

  • The JIT stands for Just In Time compilation and helps to boost the performance of Java application by converting Java byte code into native code when the crossed certain threshold i.e. mainly hot code is converted into native code.

    Java Development Kit (JDK) consists of Java Runtime Environment (JRE) along with tools to compile and debug Java code for developing Java applications.

    JRE consists of libraries, Java Virtual Machine (JVM), Java Pluging and Java Web Start to run Java applications. JRE as a stand-alone does not contain compilers and debugging tools.

detail

  1. Explain Java Heap space and Garbage collection?

When a Java process is started using java command, memory is allocated to it. Part of this memory is used to create heap space, which is used to allocate memory to objects whenever they are created in the program. Garbage collection is the process inside JVM which reclaims memory from dead objects for future allocation.

Java Heap

detail

  1. How do you find memory usage from Java program? How much percent of the heap is used

You can use memory related methods from java.lang.Runtime class to get the free memory, total memory and maximum heap memory in Java.

By using these methods, you can find out how many percents of the heap is used and how much heap space is remaining.

  • Runtime.freeMemory() return amount of free memory in bytes
  • Runtime.totalMemory() returns total memory in bytes
  • Runtime.maxMemory() returns maximum memory in bytes.
  1. What is the difference between stack and heap in Java?

Stack and heap are different memory areas in the JVM and they are used for different purposes.

  • The stack is used to hold method frames and local variables .
  • While objects are always allocated memory from the heap.
  • The stack is usually much smaller than heap memory and also didn't shared between multiple threads, but heap is shared among all threads in JVM.

Java-heap-stack

detail

  1. Oracle JDK vs OpenJDK
    1. Oracle JDK was previously called SUN JDK and that was before the takeover by Oracle. Earlier, it was the official proprietary implementation of the Java language. After the takeover it was named as Oracle JDK and Oracle’s team maintains the JDK. OpenJDK is an open source implementation of the Java Standard Edition platform with contribution from Oracle and open Java community.
    2. OpenJDK is the official reference implementation for Java Standard Edition from Java SE 7.
    3. OpenJDK is released under license GPL v2 wherein Oracle JDK is licensed under Oracle Binary Code License Agreement.

    detail