To start with the understanding of memory leaks in Java, lets first understand how memory is managed in C language. A C-programmer has to manually allocate memory to use a variable. After the variable’s task is over, that variable has to be de-allocated manually. The problem faced here is that in case the programmer forgets to unallocated the memory, it’ll remain in there and will eat up all the free memory space.
To solve this problem, Java introduced the garbage collection concept. In Java, when a developer wants to create a new variable, he doesn’t have to manually allocate memory to that variable – instead JVM (Java Virtual Machine) takes care of that. JVM regularly checks the objects in the memory to see if there are any unused variables. If the Java garbage collector finds any variable which is not in use, it’ll discard those unused variables and reclaim the memory to be used by other variables.
A Memory Leak in Java is when the garbage collector fails to identify an unused variable and hence the memory occupied by that variable is never freed. Given that any application will have limited amount of memory, consider a scenario where a large number of such variables are available in the memory and cannot be reclaimed by the garbage collector. Thus, the memory gets less & less and when the application keeps on running, there will be a time when there is no memory available to the application for creation of any new variables and finally the application will crash.
These days, memory leaks are a major problem faced by many enterprise level applications. There are a number of tools available for identifying such memory leaks. For e.g. JProfiler, JProbe etc.
If you are facing similar problem in your project where memory leaks keep shutting down your application or hampers the performance and this resulting in bad user experience, contact us for consultation. Fill in our quick contact form or contact us at firstname.lastname@example.org.