aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Memory Leaks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Memory Leaks" Watch "Memory Leaks" New topic
Author

Memory Leaks

Swati sb
Greenhorn

Joined: Feb 13, 2005
Posts: 2
What is a memory leak? Is it possible for memory leaks to occur in java and if so why n how?
Subhash Bhushan C
Ranch Hand

Joined: Jan 27, 2005
Posts: 106
Okay, since I come from the C++ legion, a Memory Leak is (from webopaedia) :
A bug in a program that prevents it from freeing up memory that it no longer needs. As a result, the program grabs more and more memory until it finally crashes because there is no more memory left.


The term "Memory Leak" is not applicable to Java, because, Java has a feature called "Garbage Collection".
To quote from the JLS (Jave Language Specification):
The Java programming language is a relatively high-level language, in that details of the machine representation are not available through the language. It includes automatic storage management, typically using a garbage collector, to avoid the safety problems of explicit deallocation (as in C's free or C++'s delete). High-performance garbage-collected implementations can have bounded pauses to support systems programming and real-time applications.


If you start learning the Java language, you will automatically understand that Java implicitly handles Object creation, memory allocation, Object destruction, memory deallocation and freeing of memory. You cannot be sure of the exact details though, because how the stuff works, is actually left to the JVM (Java Virtual Machine).

And finally, (I love this part ) repeat after me,

Java is not C++.


Regards,<br />Subhash Bhushan.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You can have a memory leak in Java if you keep references to objects after you no longer need them. For example, if you keep adding new object references to a directory during program execution but you never delete them, you can eventually run out of heap space.

The difference from C++ is that if you remove all references to a C++ object but you don't specifically do a delete() or free(), the memory is tied up until the application ends. This can happen very easily when several different objects refer to an object and you delete the last one. You may have no simple way to know that there are no other references to that object and it needs to be deleted as well. Java covers this issue by making all inaccessable "islands" of objects (referring to each other but not accessable from a live thread) eligible for automatic garbage collection.


Mike Gershman
SCJP 1.4, SCWCD in process
Subhash Bhushan C
Ranch Hand

Joined: Jan 27, 2005
Posts: 106
You can have a memory leak in Java if you keep references to objects after you no longer need them. For example, if you keep adding new object references to a directory during program execution but you never delete them, you can eventually run out of heap space.


Mike, IMHO, as webopaedia quotes, Memory Leaks are caused by bugs in program, which fail to release memory when not needed. I hope you will agree that a bug in a program, is not the same as running out of heap space because of excess of objects. That is, a Memory Leak is caused in a situation, where you have not freed the memory being used by your program, and lost the reference to it (even to free it). So, I think, strictly sticking to the meaning, Java cannot have Memory Leaks.
[ February 13, 2005: Message edited by: Agni Vartula ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Memory Leaks are caused by bugs in program, which fail to release memory when not needed. I hope you will agree that a bug in a program, is not the same as running out of heap space because of excess of objects.

So far, we agree. If the program still needed all those objects when it ran out of heap space, there was probably a basic design error, not a program bug.
That is, a Memory Leak is caused in a situation, where you have not freed the memory being used by your program, and lost the reference to it (even to free it). So, I think, strictly sticking to the meaning, Java cannot have Memory Leaks.

That is just one example. If the programmer never even tries to get rid of unneeded objects in a high volume 24/7 application, that is also a bug that may cost the client many thousands of dollars in sales. What is worse, it is a bug that may not even show up in volume testing.

At least in Java, it is much easier to track down which objects are occupying all that memory. In C++, you have to either analyze a trace or look at raw memory and try to identify the objects. In Java, all objects have identifying information inserted by the class loader.

Of course, you can say that failing to delete unneeded objects is not what you mean by a memory leak.

'When I use a word,' Humpty Dumpty said in a rather scornful tone, 'it means just what I choose it to mean - neither more nor less.' -- Lewis Carroll, Through the Looking-Glass


Somewhat off topic:
I was working in the corporate network group when a colleague installed a new fiber ring connecting the main buildings. Something went wrong and it stopped working for hours. My boss pointed out that the fiber ring was not down, it was just not passing any data. Some users have no sense of humor!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

A bug in a program that prevents it from freeing up memory that it no longer needs. As a result, the program grabs more and more memory until it finally crashes because there is no more memory left.


Given the basis for the definition of a memory leak as this thread defines it, Java is capable of leaking memory. There are typically two ways of doing so: holding onto an external resource, such as a file or socket; object creation without release. I believe I gave an example of the former in this forum a few weeks ago (calling InputStream.close() in anything but a finally block is almost certainly a leak), so I'll provide an example of the latter (taken directly from my FAQ http://www.xdweb.net/~dibblego/java/faq). You'll also recall a nasty leak in java.lang.StringBuffer a few years back (forget the exact version) that occurred after calls to the delete method and was of a similar nature to the following:



Tony Morris
Java Q&A (FAQ, Trivia)
Subhash Bhushan C
Ranch Hand

Joined: Jan 27, 2005
Posts: 106
Thanks Tony, good example.

Hey Mike,
'When I use a word,' Humpty Dumpty said in a rather scornful tone, 'it means just what I choose it to mean - neither more nor less.' -- Lewis Carroll, Through the Looking-Glass


Nice one.. It entered my books yesterday.. TY.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Leaks