JVM INTERNALS – Expert
- 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.
- 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.
- What is the size of an int variable in 32-bit and 64-bit JVM?
The size of int is same in both 32-bit and 64-bit JVM, it’s always 32 bits or 4 bytes.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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 and Runtime.maxMemory() returns maximum memory in bytes.