Can there be memory leak in Java

2019-01-17 09:49发布

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

12条回答
劳资没心,怎么记你
2楼-- · 2019-01-17 10:38

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();
    }
}
查看更多
别忘想泡老子
3楼-- · 2019-01-17 10:41

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-17 10:44

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.

查看更多
男人必须洒脱
5楼-- · 2019-01-17 10:46

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.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-17 10:47

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

查看更多
聊天终结者
7楼-- · 2019-01-17 10:48

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.
查看更多
登录 后发表回答