Skip to main content

Posts

Showing posts with the label Memory Leak

Memory Leak in Java? Does 'static' cause it?

Does 'static' cause Memory Leak in Java? What's memory leak? In simple terms, it's unused but referenced (somehow because the programmer probably unintentionally forgot to remove the references once the use of the object was over) part of the memory. Before we start discussing if 'static' can cause memory leak in Java, let me assure you that whatever you've read about Garbage Collectors in Java, is still valid and it certainly takes care of most (almost all) of the memory allocation/reclaimation of Java objects. But, that alone doesn't remove the possibility of the presence of memory leak in a Java program - just for example, you might not only be using only Java objects in your Java program. Putting it differently, what if you have used some native objects and forgot to reclaim the memory explicitly because that's anyway not going to be taken care by the GC (which takes care of heap memory management only)... right? Now that we agree with the pos...

Can a Java application have memory leak?

Yes, there could be memory leak in Java applications. Wait a minute, doesn't Java virtual machine have a garbage collector that will collect and free all unreferenced memory automatically? Let's find out in general what memory leaks are, and how they occur in applications. If an application fails to return the not-in-use memory back to the heap, the "lost" memory is called memory leak. Memory leaks occur when the application doesn't free the memory allocated, usually are the objects no longer in use, but the object references are lost. If an object is no longer accessible, there is no way to free its memory. Each time such a leak is re-created, additional memory is used and not freed. Eventually, the process that runs the application will run out of memory and crash. It's true that for other programming languages, such as C/C++, there is not such a thing called garbage collector. The programmer is responsible for freeing the memory when the ...