150+ Java Interview Questions for Experienced Developers - Part 3 (JVM Internals and GC)
150+ Java Interview Questions That Separate $100K from $200K Engineers
Coding Interview coming up? Join ByteByteGo now for a structured preparation. They are also offering a rare 50% discount now on their lifetime plan
Hello guys, this is the third part of my mega series of Java interview questions, 150+ Java Interview Questions for Experienced Developers. In the first part, I have shared frequently asked Java interview questions on Multithreading and Concurrency and in the 2nd part, I have covered core Java fundamentals.
In this, 3rd part, I will cover JVM architecture, how it works, Garbage collection and Java performance tuning, which is very important topic for senior engineers with 5 to 10 years experienced.
If you are preparing for Java interview or Java certification then knowing these core concepts will help you a lot.
I have also written a book for Java interviews, Grokking the Java Interview, and Grokking the Spring Boot Interview, where I have shared tips, tricks and frequently asked Java questions from different topics. If you are preparing for Java interviews, then definitely check them out. You can also use code friends20 to get 20% discount now.
š£Coursera 50% OFF (Sponsored)
Level up your AI skills with Coursera Plus at 50% off, get a full year for just $199 (normally $399). The offer ends Jan 29, making this a great time to start learning AI, Machine Learning, and other in-demand tech skills from top universities and companies.
JVM Internals and Garbage Collection Interview Questions
In last a couple of years I have seen increased focus on JVM internal and Garbage collection tuning, monitoring Java application, dealing with Java performance issues on various Java interviews.
This is actually become the prime topic for interviewing any experienced Java developer for senior position e.g. technical lead, VP or team lead.
If you feel you are short of experience and knowledge in this area then you should read at least one book mentioned in my list of Java Performance books. I vote goes to Java Performance, The Definitive guide by Scott.
Now, letās start with the questions :
31) 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.
32) 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.
33) 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.
34) 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.
35) How do WeakHashMap works? (answer)
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.
36) 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.
37) 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
38) 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 ²³² 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 ²ā¶ā“ 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.
39) 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.
40) 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.
41) Can you guarantee the garbage collection process? (answer)
No, you cannot guarantee the garbage collection, though you can make a request using System.gc() or Runtime.gc() method.
42) 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 precents 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.
43) 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.
Thatās all about the core Java interview questions part 3 guys. In the first part, we have seen multithreading and concurrency and in second part we have focused on core Java fundamentals like casting and operators.
In this part, I have covered Garbage Collection and JVM internals interview questions, and in next part, I am going to share another interesting topic for Java interviews. so stay tuned for next part. You can also share this post to your friends who are preparing for Java interviews, its free.
In case you donāt know, I have also written a book for Java interviews, Grokking the Java Interview, and Grokking the Spring Boot Interview, where I have shared tips, tricks and frequently asked Java questions from different topics.
You can read the book to better prepare for your Java interviews. You can also use the code ā friends20 to get a 20% discount because you are already my reader.
Other Java and Spring Boot Interview Questions you may like











Solid compilation of JVM fundamentals. The question about WeakReference vs SoftReference is particuarly useful because its one of those concepts that comes up rarely but when it does, proper understanding can prevent major memory leaks. Ran into a situation last year where a caching layer was eating memory, and it turned out to be misused SoftReferences that wehrent being collected under normal conditions. Worth understanding when JVM actually decides to clear them