javaguy
javaguy

Reputation: 4432

Can there be memory leak in Java

I get this question asked many times. What is a good way to answer

Upvotes: 35

Views: 22041

Answers (12)

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74360

The short answer:

A competent JVM has no memory leaks, but more memory can be used than is needed, because not all unused objects have been garbage collected, yet. Also, Java apps themselves can hold references to objects they no longer need and this can result in a memory leak.

Upvotes: 3

Johnny
Johnny

Reputation: 15413

Yes, it can be, in a context when a program mistakenly hold a reference to an object that would be never used again and therefore it's not cleaned by the GC.

An example to it would be forgetting to close an opened stream:

class MemoryLeak {

    private void startLeaking() throws IOException {
        StringBuilder input = new StringBuilder();
        URLConnection conn = new URL("www.example.com/file.txt").openConnection();

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));        

        while (br.readLine() != null) {
            input.append(br.readLine());
        }
    }

    public static void main(String[] args) throws IOException {
        MemoryLeak ml = new MemoryLeak();
        ml.startLeaking();
    }
}

Upvotes: 2

Stephen C
Stephen C

Reputation: 718778

Can there be memory leak in Java?

The answer is that it depends on what kind of memory leak you are talking about.

Classic C / C++ memory leaks occur when an application neglects to free or dispose an object when they are done with it, and it leaks. Cyclic references are a sub-case of this where the application has difficulty knowing when to free / dispose, and neglects to do it as a result. Related problems are where the application uses an object after it has been freed, or attempts to free it twice. (You could call the latter problems memory leaks, or just bugs. Either way ... )

Java and other (fully1) managed languages mostly don't suffer from these problems because the GC takes care of freeing objects that are no longer reachable. (Certainly, dangling pointer and double-free problems don't exist, and cycles are not problematic as they are for C / C++ "smart pointers" and other reference count schemes.)

But in some cases GC in Java will miss objects that (from the perspective of the programmer) should be garbage collected. This happens when the GC cannot figure out that an object cannot be reached:

  • The logic / state of the program might be such that the execution paths that would use some variable cannot occur. The developer can see this as obvious, but the GC cannot be sure, and errs on the side of caution (as it is required to).
  • The programmer could be wrong about it, and the GC is avoiding what might otherwise result in a dangling reference.

(Note that the causes of memory leaks in Java can be simple, or quite subtle; see @jonathan.cone's answer for some subtle ones. The last one potentially involves external resources that you shouldn't rely on the GC to deal with anyway.)

Either way, you can have a situation where unwanted objects cannot be garbage collected, and hang around tying up memory ... a memory leak.

Then there is the problem that a Java application or library can allocate off-heap objects via native code that need to be managed manually. If the application / library is buggy or is used incorrectly, you can get a native memory leak. (For example: Android Bitmap memory leak ... noting that this problem is fixed in later versions of Android.)


1 - I'm alluding to a couple of things. Some managed languages allow you to write unmanaged code where you can create classic storage leaks. Some other managed languages (or more precisely language implementations) use reference counting rather than proper garbage collecting. A reference count-based storage manager needs something (i.e. the application) to break cycles ... or else storage leaks will ensue.

Upvotes: 25

Trivikram
Trivikram

Reputation: 851

The book Effective Java gives two more reasons for "memory leaks":

  • Once you put object reference in Cache and forget that it's there. The reference remains in cache long before becoming irrelevant. Solution is to represent cache as a WeakHashMap
  • in an API where clients register callbacks and don't re-register them explicitly. Solution is to store only weak references to them.

Upvotes: 2

AnthonyJoeseph
AnthonyJoeseph

Reputation: 323

Well, considering that java uses a garbage collector to collect unused objects, you can't have a dangling pointer. However, you could keep an object in scope for longer than it needs to be, which could be considered a memory leak. More on this here: http://web.archive.org/web/20120722095536/http://www.ibm.com:80/developerworks/rational/library/05/0816_GuptaPalanki/

Are you taking a test on this or something? Because that's at least an A+ right there.

Upvotes: 10

vz0
vz0

Reputation: 32923

Yes. A memory leak is unused memory not released to the memory manager by the app.

I've seen many times Java code wich stores items on a data structure but the items are never removed from there, filling the memory until an OutOfMemoryError:

void f() {
    List<Integer> w = new ArrayList<Integer>();
    while (true) {
         w.add(new Integer(42));
    }
}

While this example is too obvious, Java memory errors tend to be more subtle. For example, using Dependency Injection storing a huge object on a component with SESSION scope, without releasing it when the object is no longer used.

On a 64 bits VM this tends to get worse since the swap memory space starts to get filled until the system crawls on too many IO operations.

Upvotes: 0

jonathan.cone
jonathan.cone

Reputation: 6706

The answer is a resounding yes, but this is generally a result of the programming model rather than an indication of some defect in the JVM. This is common when frameworks have lifecycles different of that than a running JVM. Some examples are:

  1. Reloading a context
  2. Failing to dereference observers (listeners)
  3. Forgetting to clean up resources after you're finished using them *

* - Billions of consulting dollars have been made resolving the last one

Upvotes: 6

Swaranga Sarma
Swaranga Sarma

Reputation: 13413

One simple answer is : JVM will take care of all your initialization of POJO's [plain old java objects] as long as you are not working with JNI. With JNI if you have made any memory allocation with the native code you have to take care of that memory by yourself.

Upvotes: 0

Markus Johnsson
Markus Johnsson

Reputation: 4019

Yes. Memory leaks can still occur even when you have a GC. For example, you might hold on to resources such as database result sets which you must close manually.

Upvotes: 11

Tyler
Tyler

Reputation: 2879

Yes, in the sense that your Java application can accumulate memory over time that the garbage collector is unable to free.

By maintaining references to uneeded/unwanted objects they will never fall out of scope and their memory will not be claimed back.

Upvotes: 4

dteoh
dteoh

Reputation: 5922

Yes it is possible.

In Effective Java there is an example involving a stack implemented using arrays. If your pop operations simply decrement the index value it is possible to have a memory leak. Why? Because your array still has a reference to the popped value and you still have a reference to the stack object. So the correct thing to do for this stack implementation would be to clear the reference to the popped value using an explicit null assignment at the popped array index.

Upvotes: 3

pstanton
pstanton

Reputation: 36640

yes, if you don't de-reference objects they will never be garbage-collected and memory usage will increase. however because of how java is designed, this is difficult to achieve whereas in some other languages this is sometimes difficult not to achieve.

edit: read Amokrane's link. it's good.

Upvotes: 3

Related Questions