File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Does Java have buffer overflows and memory leaks? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Does Java have buffer overflows and memory leaks?" Watch "Does Java have buffer overflows and memory leaks?" New topic
Author

Does Java have buffer overflows and memory leaks?

Val Pecaoco
Ranch Hand

Joined: Dec 05, 2001
Posts: 156
Hi Ranchers,
Since Java is written in C++ (more details on this link), does it mean that Java may have buffer overflows and memory leaks? (Which may explain a Volano security advisory report that some Java VM implementations are vulnerable to a denial-of-service attack.)
Any opinions/statements of facts about this?
Ex Animo Java!
-- Val
[ August 28, 2002: Message edited by: Val Pecaoco ]

"Knowledge is power, but enthusiasm is the key." -- Lavern Barn
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Memory leaks are not a property of any one language -- not even C++.
It is certainly possible to create memory leaks in a running Java program, but it has nothing to do with native code libraries. It has everything to do with careless programming.
Val Pecaoco
Ranch Hand

Joined: Dec 05, 2001
Posts: 156
Originally posted by Michael Ernest:
It is certainly possible to create memory leaks in a running Java program, but it has nothing to do with native code libraries. It has everything to do with careless programming.

So, it's like, a carelessly-programmed Java virtual machine in C++, for example, may be a potential source of memory leaks?
sridhar satuloori
Ranch Hand

Joined: Nov 05, 2001
Posts: 144
Originally posted by Michael Ernest:
Memory leaks are not a property of any one language -- not even C++.
It is certainly possible to create memory leaks in a running Java program, but it has nothing to do with native code libraries. It has everything to do with careless programming.

Michael,
My understanding is that Java never creates a memory leak how bad your program could be. If at all if there are any memory leaks they are because oof the faulty implementation of JVM.
It is certainly possible to create memory leaks in a running Java program
I am just curious here. can you write a single example which creates a memory leak on any JVM.
--sridhar
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Basically the thing to keep in mind is that Sun's implementatation of Java is really good. There are a few bugs in the standard libraries, but I can count on the fingers of one hand the number of times that Sun's virtual machine has unexpectedly halted running a pure Java application -- and I've been using java since 1.1. Just go to securityfocus.net and look for security bugs in Sun's virtual machine.
In response to sridhar:
You can maintain references to unused objects and clog the memory -- read: you can create memory leaks.
Here's an example of a memory leak that's in Effective Java by Joshua Bloch -- an array based stack. I know it's a good example because I saw with my own eyes a grad student instructor make this exact same stack leak mistake this summer.

Do you see the memory leak?
It's in the pop() method. Once I put something into the stack, it won't be garbage collected until the entire stack is. I don't delete the reference from the array -- I just change a pointer.
Here's the fix:

It's just important to remember that when an object isn't going to be used ever again, any references to it should either go out of scope at the end of the current code block or be specifically set to null.
[ August 29, 2002: Message edited by: David Weitzman ]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Val Pecaoco:

So, it's like, a carelessly-programmed Java virtual machine in C++, for example, may be a potential source of memory leaks?

Like, that's totally not my point, but you're not wrong either?
sridhar satuloori
Ranch Hand

Joined: Nov 05, 2001
Posts: 144
thanks, Michel and David,
Now i accept that u can create a memory leaks in java. I was under impression that Java doesnt allow you to create "memory leak".
--sridhar
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Those of us who are C++ programmers can tell you that the example is not a true memory leak in the classic term. Back in the old days we used to write programs that would never free memeory even after they terminated. You'd have to reboot your windows PC to get your memory back. Now that was a memory leak!!!


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Isn't C++ memory reclaimed when the program exits?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by David Weitzman:
Isn't C++ memory reclaimed when the program exits?

Not in the old days when we ran on Win 3.1! We had one program that would run for about 2 hours and then lock up a Win 3.1 PC. The only fix was Ctrl-Alt-Delete!
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But when a C or C++ program exits, doesn't its entire address space go away because the process exits?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You obviously never worked with 16 bit versions of Windows!
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Nope -- my C and C++ work was all on various flavors of Unix.
 
jQuery in Action, 2nd edition
 
subject: Does Java have buffer overflows and memory leaks?