Easiest way to cause memory leak in Java? [duplica

2019-01-30 19:01发布

Possible Duplicate:
Creating a memory leak with Java

What's the easiest way to cause a java memory leak?

9条回答
该账号已被封号
2楼-- · 2019-01-30 19:47
public static List<byte[]> list = new ArrayList<byte[]>();

And then add (big) arrays without removing them. At some point you will run out of memory without suspecting. (You can do this with any objects, but with big, full arrays you can run out of memory faster)

In Java, if you dereference an object (it falls out of scope), it is garbage-collected. So you have to hold a reference to it in order to have a memory problem.

查看更多
贪生不怕死
3楼-- · 2019-01-30 19:57

It does seem that most of the answers are not C style memory leaks.

I thought I'd add an example of a library class with a bug that will give you an out of memory exception. Again, it is not a true memory leak but is an example of something running out of memory that you would not expect.

public class Scratch {
    public static void main(String[] args) throws Exception {
        long lastOut = System.currentTimeMillis();
        File file = new File("deleteme.txt");

        ObjectOutputStream out;
        try {
            out = new ObjectOutputStream(
                    new FileOutputStream("deleteme.txt"));

            while (true) {
                out.writeUnshared(new LittleObject());
                if ((System.currentTimeMillis() - lastOut) > 2000) {
                    lastOut = System.currentTimeMillis();
                    System.out.println("Size " + file.length());
                    // out.reset();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class LittleObject implements Serializable {
    int x = 0;
}

You will find the original code and bug description at

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4363937

查看更多
Melony?
4楼-- · 2019-01-30 19:58

You cannot really "leak memory" in Java unless you:

  • intern strings
  • generate classes
  • leak memory in the native code called by jni
  • keep references to things that you do not want in some forgotten or obscure place.

I take it that you are interested in the last case. The common scenarios are:

  • listeners, especially done with inner classes
  • caches.

A nice example would be to:

  • build a Swing gui that launches a potentially unlimited number of modal windows;
  • have the modal window do something like this during its initialization:
    StaticGuiHelper.getMainApplicationFrame().getOneOfTheButtons().addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
         // do nothing...
      }
    })
    

The registered action does nothing, but it will cause the modal window to linger in memory forever, even after closing, causing a leak - since the listeners are never unregistered, and each anonymous inner class object holds a reference (invisible) to its outer object. What's more - any object referenced from the modal windows have a chance of leaking too.

This is why libraries such as EventBus use weak references by default.

Apart from listeners, other typical examples are caches, but I cannot think of a nice example.

查看更多
登录 后发表回答