Wednesday, July 9, 2025

How to Fix java.lang.OutOfMemoryError: GC overhead limit exceeded ? Example Solution

The java.lang.OutOfMemoryError: GC overhead limit exceeded is another type of OutOfMemoryError in Java which comes when JVM spent too much time doing garbage collection without any success.  For example, if almost 98% of CPU for a Java process is busy doing GC and reclaims very less amount of Java heap space around 2%, then JVM throws "java.lang.OutOfMemoryError: GC overhead limit exceeded" error. Though, the definition of 98% CPU time may vary between different Garbage collectors and different JVM versions.  

Thursday, June 12, 2025

Difference between Stack and Heap memory in Java? Example

The difference between stack and heap memory is a common programming question asked by beginners learning Java or any other programming language. Stack and heap memory are two terms programmers start hearing once they started programming but without any clear and definite explanation. Lack of knowledge of what is a heap in Java and what is stack memory in Java results in misconceptions related to stack and heap. To add to this confusion, a stack is also a data structure that is used to store elements in LIFO(Last In First Out) order and is available in Java API as java.util.Stack.

Saturday, April 5, 2025

ThreadLocal Memory Leak in Java web application - Tomcat

ThreadLocal variables are infamous for creating memory leaks. A memory leak in Java is the amount of memory hold by an object which is not in use and should have been garbage collected, but because of unintended strong references, they still live in Java heap space. There are many ways memory leak can be caused in Java but when this memory leak is caused due to the ThreadLocal variable, it’s referred to as ThreadLocal memory leak. In our last post about the ThreadLocal variable, we have seen How the ThreadLocal variable can make SimpleDateFormat thread-safe and also raised points that in a managed environment like the J2EE application server or a web servers like Tomcat, Jetty, WebSphere or Weblogic use of ThreadLocal should be avoided.

How to get max memory, free memory and total memory in Java? Example

Getting free memory, total memory and max memory on JVM is using Runtime class in Java. and many java programmers is interested to know whether they have any relationship with JVM argument -Xms and -Xmx which is used to specify starting heap size and maximum heap size of JVM. I have touched this on 10 Points on Java heap and how to set heap size in ANT and Maven but here we will see some way to find out starting and maximum heap size from Java program.

Wednesday, April 2, 2025

How to find if JVM is 32 or 64 bit from Java program? Example

You can find JVM bit sizes like 32 bit or 64 bit by using either running java command from the command prompt or by using System.getProperty() from Java program. The question is why do you want to know hardware or platform configuration while writing Java code which is supposed to write once and read anywhere(32 bit, 64 bit, etc)? Yes we don't really need to know whether JVM is 32 bit or 64 bit more often but there are many situations when this matters

Tuesday, February 18, 2025

Top 5 Udemy Courses to learn JVM Internals, Memory Management, GC and Performance Tuning in Java in 2025

For a senior Java developer, it's essential to know how JVM works and how to troubleshoot issues with respect to memory, most notably memory leaks in Java applications and servers like Tomcat. You might be thinking, how come memory leaks in Java? Isn't memory is managed by JVM and Garbage collector? Well, that's true, but poor coding or just a bit of carelessness can cause memory leaks in Java. If you don't know how to configure JVM, troubleshoot memory-related problems on the heap, and stack, you will struggle at a higher level. That's why it's essential for experienced Java developers to spend some time learning these advanced skills as their experience grows.

Tuesday, April 23, 2024

4 Ways to take Heap dump in Tomcat and Java and 3 Tools to Analyse .hprof files

The heap dump is a great source of information on identifying memory related issues on Java application e.g. web and application servers like Tomcat, WebLogic, JBoss, WebSphere and IDEs like IntelliJIDEA and Eclipse. When you take heap dump all live objects of heap with their count and other details are written into a huge file (the .hprof file) for analysis. The name and location of heap dump file is provided by command you use to take the heap dump. There are many command line tools in JDK e.g. jmap and jcmd which can be used to take the heap dump in Java. 

Wednesday, January 24, 2024

Top 5 Java Performance Tuning Books for Experienced Programmers - Best of Lot, Must read

You might be thinking, why should a Java developer read a book on Performance tuning? When I first faced this question a long time back, I thought I will do it later, but I never got back to that for a long time. I realize my mistake of having a lack of knowledge on memory measurement, JVM tuning, and finding bottleneck only when I faced severe performance and scalability issues on our mission-critical, server-side financial application written in Java. It's true that when you really need it, you learn most, but those times are not the best time to learn fundamentals, in fact, those times are to apply and correct your misunderstanding.

Monday, September 11, 2023

Difference between WeakReference vs SoftReference vs PhantomReference in Java? Example

WeakReference and SoftReference were added into Java API for a long time but not every Java programmer is familiar with it. This means there is a gap between where and how to use WeakReference and SoftReference in Java. Reference classes are particularly important in the context of How Garbage collection works. As we all know that Garbage Collector reclaims memory from objects which are eligible for garbage collection, but not many programmers know that this eligibility is decided based upon which kind of references are pointing to that object. This is also the main difference between WeakReference and SoftReference in Java.

Thursday, April 27, 2023

Difference between -Xms and -Xmx JVM Parameters forJava Heap Memory

Though both -Xms and -Xmx JVM options are used to specify the size of heap memory, main difference between them is that -Xms is used to specify initial heap memory while -Xmx is used to specify the maximum heap memory. The JVM (Java virtual Machine) provides several command line arguments to specify size of different memory areas, also known as generations of Java Heap space, but two of the most useful JVM parameters which specify initial and maximum heap memory is -Xms and -Xmx. When you start JVM by using java command you can specify the maximum heap size with the JVM option -Xmx and a size e.g. java -Xmx1G MainClass will start JVM with maximum heap size of 1 giga bytes. Remember there is no space between -Xmx and size, if you leave space e.g. -Xmx 1024M then JVM will throw invalid heap memory error, so be careful there. 

Wednesday, April 19, 2023

Difference between 32-bit and 64-bit JVM in Java?

One of the rather new or latest Java interview question is, "What is the difference between 32-bit and 64-bit JVM? or, How do you choose between 32-bit vs 64-bit JVM? or When to use 32-bit or 64-bit JVM? Some interviewer also use this concept to trick candidates by asking what would be the size of int variable in 32-bit and 64-bit Java? As mentioned in 130+ Java questions from last 5 years. Another tricky questions is, does same Java program can run on both 32-bit and 64-bit JVM? Short answer of this question is that Java is platform independent and same Java program will run absolutely fine in both 32-bit and 64-bit JVMThe real difference comes in addressable memory or heap memory

Monday, January 31, 2022

How Garbage Collection works in Java? Explained

I have read many articles on Garbage Collection in Java, some of them are too complex to understand and some of them don’t contain enough information required to understand garbage collection in Java. Then I decided to write my own experience as an article. You can call it a tutorial about garbage collection in simple words, which would be easy to understand and have sufficient information to understand how garbage collection works in Java. Garbage collection works by employing several GC algorithms like Mark and Sweep, G1, etc. There are different kinds of garbage collectors available in Java to collect different areas of heap memory like you have serial, parallel, and concurrent garbage collectors in Java.

Friday, July 30, 2021

5 Coding Tips for Improving Performance of Java application

This is a big topic to discuss as there are many approaches that involve analyzing the performance of an application starting from profiling the application to finding the bottleneck.

here I am putting some of the basic tricks for improving performance which I learned in my early days in java, I will keep posting some approaches, experience on performance improvement as and when time allows.

for now, here are naive's tips for making your program run faster.

When a class is loaded and initialized in JVM - Java Example

Understanding when a class is loaded and initialized in JVM is one of the fundamental concepts of Java programming language. Thanks to Java language specification we have everything clearly documented and explained, but many Java programmer still doesn't know when a class is loaded or when a class is initialized in Java. Classloading and initialization seems confusing and complex to many beginners and its true until having some experience in belt its not always easy to get into subtle details of How JVM works in Java. In this Java tutorial, we will see when class loading occurs in Java and when and how class and interface are initialized in Java.

Java ArrayList and HashMap Performance Improvement in Java

For a long time, one reason for me to update to the newer Java version was always bug fix and performance improvement. Apart from major changes like Generics in Java 1.5 and Lambdas in Java 8, there are so many small improvements, performance optimization which just goes under the radar, one such change is creating empty ArrayList and HashMap with size zero in JDK 1.7.0_40 update. Many Java developer doesn't even know about these changes, part of the blame lies on Java developers like me, as I hardly read release notes of minor Java updates. 

Invalid initial and maximum heap size in JVM - How to Fix

I was getting "Invalid initial heap size: -Xms=1024M" while starting JVM and even after changing the maximum heap size from 1024 to 512M it keeps crashing by telling "Invalid initial heap size: -Xms=512m, Could not create the Java virtual machine". I check almost everything starting from checking how much physical memory my machine has to any typo in JVM parameters, only to find out that instead of M, I had put MB there. Java accepts both the small case and the capital case for Kilo, Mega, and Gigs. you can use m or M, g or G, etc but never used MB, GB, or KB.  A similar problem can occur with the maximum heap size specified by -Xmx. Also from Java 6 update 18, there is a change in default heap size in JVM.  

10 Examples of HotSpot JVM Options in Java

There are hundreds of JVM parameters or JVM Options exists inside sun JDK and its virtually impossible to keep track of every single JVM option and based on my experience we don't even use most of JVM flags except a couple of important JVM option related to java heap size, java options for printing garbage collection details and most likely JVM switches for setting up remote debugging in Java. but there are many other useful categories of JVM parameters that you at least like to be familiar with even if not intending to use them more frequently.

Difference between Sun (Oracle) JVM and IBM JVM?

There are different JVM implementations available apart from popular Sun's (now Oracle) hotspot JVM like IBM's JVM. Now the question comes, what makes two JVM different from each other? Quite a lot of things can be different even if two JVMs are implemented by following Java virtual machine specifications. IBM has its own JVM implementation which is different than Oracle's JVM in many things like JIT(Just in Time) Compiler, the structure of Java heap, garbage collectors, and support for monitoring and management. Though I have mostly used Sun’s hotspot JVM recently got a chance to run some Java programs on IBM JVM on the AIX platform.

How to find CPU and Memory used by Java process in Solaris – prstat command example

We often need to find CPU and memory utilization of a particular Java process in order to get some performance stats, capacity planning or just to understand the current load on the system. Performance monitoring and testing is an integral part of any Java application development, especially if you are working in a high-frequency trading space. In order to fix a performance problem, you have to find them first and knowledge of the right tools and commands helps there. One of the primary causes of poor application performance is it Java or any other process is the exhaustion of important resources like CPU and memory.

How ClassLoader Works in Java? Example

Java class loaders are used to load classes at runtime. ClassLoader in Java works on three principles: delegation, visibility, and uniqueness. Delegation principle forward request of class loading to parent class loader and only loads the class if the parent is not able to find or load the class. The visibility principle allows the child class loader to see all the classes loaded by the parent ClassLoader, but the parent class loader can not see classes loaded by a child. The uniqueness principle allows one to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by a parent. Correct understanding of class loader is a must to resolve issues like NoClassDefFoundError in Java and java.lang.ClassNotFoundException, which is related to class loading.