Memory Leak in Java
Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically destroys the item and makes room for new applications if it is no longer needed by the programme. Thus, unlike other programming paradigm languages (C and C++), the programmer is not required to manually manage memory. Nevertheless, a Java application could experience a memory leak. This section explains memory leaks in Java, their causes, how to find them, and how to remedy them.
What is memory leak in Java?
Whenever the garbage collector fails to identify useless objects, they remain in memory indefinitely and cause a memory leak in Java, which lowers the amount of capacity allotted to the programme. Because unused objects are still being referenced, OutOfMemoryError may occur. It also has an impact on the application's dependability. The memory leak is depicted in the following diagram.
For the application, we can define the minimum and maximum heap sizes. The two variables (options) for configuring the heap size are as follows:
- -Xms <size> m
- -Xmx <size> m
m stands for the size in MB.
Symptoms of Memory Leak
Whether any Java application has a memory leak, you should report it to the following:
- The application's performance is steadily declining.
- Throughout an application's lifespan, memory utilisation rises.
Causes of Memory Leaks
Memory leaks within Java can be caused by the following things:
Making Use of Unwanted Object References: These are object mentions that are no longer required. Because another object is still making references to the undesirable object, the garbage collector isn't able to free up the memory.
Using Long-live Static Objects: Using static objects with a long lifespan causes a memory leak as well. because they remain in the memory for the duration of the application.
Absence of clean up Native System Resources (Native System Resources): Native Following Three types allocated by a method outside of Java. C and C++ were used to write it. Java code can incorporate native libraries thanks to JNI APIs.
Bugs in the Third-party Libraries: Another reason for memory leaks is bugs in third-party libraries, including the AWT & Java Swing packages.
Preventing Memory Leak
Keep the following in mind while you write Java code to avoid memory leaks.
- Don't make things that are not necessary.
- Avoid concatenating strings.
- Implement String Builder.
- Don't fill the session up with a lot of data.
- When the session is no longer needed, time it out.
- Use of System.gc() is not advised.
- Avoid using stationary things. since they automatically live for the duration of the application. Therefore, it is preferable to explicitly set the connection to null.
- In the finally block, always shut down the ResultSet, Comments, and Connection objects.
Detecting Memory Leak
Finding buffer overflows is a challenging process. There are numerous tools that do static analysis and find memory leaks, which makes the process easier:
- JProbe
- Jprofiler
- AppPerfect
- JRockit
- Visual VM
- YourKit
- GCeasy
Fixing Memory Leak
The following are remedies for the memory leak issue:
Using JVM Tools: A variety of tools are available for code optimization and memory status display.
Using Heap Dump: The approach of Heap Dump is the answer to the issue of memory leaks. It is a picture of every item in the mind at a specific moment. Additionally, it improves how much memory a Java application uses. It is kept in hprof in binary format.
Eclipse Capacity Leak Warnings: When developing a Java application using the Eclipse framework, eclipse frequently displays warnings and problems anytime it runs into memory leak-related issues.
Creating Memory Leak
Let's write a straightforward Java programme that leaks memory.
Memory.java
import java.util.Vector;
public class Memory
{
public static void main (String[] args)
{
Vector obj1 = new Vector (314567);
Vector obj2 = new Vector (876543987);
System.out.println ("This software has no memory leaks.");
}
}

In the program above, we created two Vector entities and supplied them with huge integers. When we run the aforementioned software, java.lang.OutOfMemoryError is displayed. because it takes up no room in the memory. Whenever a programme publishes a statement, this application verifies that there are no memory leaks and that it will function properly.