Learn how memory leaks manifest themselves in Java and how to avoid a System Out of Memory Exception.
Contrary to what many people think, an application written in Java may rather present memory leak problems, resulting in exceptions of type System Out of Memory (java.lang.OutOfMemoryError).
Unfortunately, a large number of Java programmers think that Memory leak is a C / C ++ thing and that the Java garbage collector solves this problem completely, but the garbage collector, although it works very well, is not capable of doing magic.
Usually, memory leak can be of two types:
- Memory blocks are allocated and available for use by the application, but are not accessible because the application has no pointer pointing to this memory area. Therefore, these memory blocks can neither be used by the application nor by other processes.
- Memory blocks have data that could be released because they are inaccessible and that, by "forgetting", are still referenced in the code even though they are not being used, and can not be released.
These two types of memory leak, if not treated could generate a System Out of Memory Exception.
The first situation is very common in C / C ++ programs. Allocate an amount of memory using the malloc2 function, for example, and in sequence the programmer causes the pointer to that memory area to point to another location, losing the initial reference. In Java this type of Memory leak does not occur because the garbage collector is able to detect allocated and unreferenced blocks of memory and release them for later use.
But it is in second situation that the problem exists for Java programmers.
One of the great advantages of Java over lower-level programming languages is the presence of the garbage collector. The function of it is to scour the memory behind allocated blocks that can no longer be referenced by the application. When the garbage collector encounters such a situation, it is deallocated to make it available for use by the application again, avoiding a System Out of Memory Exception.
After understanding what a Memory leak is and how the Java garbage collector works, let's now show you how to cause a Memory leak in Java and what to do to avoid it.
Firstly, I would like to stress that Memory leaks are difficult to discover (sometimes external tools are required) and are always caused by programming errors. Usually programmers do not care much about them, until they start consuming an excessive amount of memory or even dropping the JVM (when the memory runs out, the JVM throws a java.lang.OutOfMemoryError and ends).
The garbage collector is able to detect unreferenced objects and destroy them, releasing memory. To create a Memory leak, simply keep the reference to one or more objects in the code, even without using it later. This way the garbage collector can never destroy objects, and they will continue to exist in memory even though they are no longer accessible.If you want to avoid a System Out of Memory Exception, delete all references to unnecessary objects. By doing this, the garbage collector will be able to do its job completely and you will be free of memory leaks in your applications.