举一个memory leak的例子
不要举资源访问以及JNI方面的例子

解决方案 »

  1.   

    http://forum.java.sun.com/thread.jspa?forumID=4&threadID=456545
    实际上,是不存在memory leak的,这种误解其实大多原自中文跟英文的对应。
      

  2.   

    实际上,你问的应该是下面的描述,上面的地址里面有详细的贴子。注意下面的'leak'是加了引号的For example, you have a window A with a button on it. Through some mechanism, the user can open a new instance of another window B. B adds itself as a listener to the button on A. The developer has set the defaultCloseOperation of B to DISPOSE_ON_CLOSE. When the window is closed, it disapears and there is no way to get it back. However, the button on A still has a reference to the B instance as a listener. As long as A is around, no instance of B will ever be GC'd. If the user keeps opening and closing B windows, you will have an apparent 'leak' of memory.
      

  3.   

    Effective java 有介绍 ,比如存在数组中的对象,虽然丢弃句柄,但是对象是不会被垃圾收集的
      

  4.   

    我十分不同意楼上的不负责任的回复
    java的确不需要我们去管理内存,比如分配,释放,但并不代表不存在这种情况
    而我的项目,在进行压力测试,内存高据不下,只到内存用尽
    最终Outof Memory
      

  5.   

    对不起 mysticality(影子传说) 
    我所说的楼上是samismile(米兰10年)
      

  6.   

    对于没有被引用的对象是可以回收的.但如果'持有对无用对象的引用',那么就memory leak了.public class Stack 
    {    
    private Object[] elements;
    private int size = 0;

    public Stack(int initialCapacity) 
    {
    this.elements = new Object[initialCapacity];
    }

    public void push(Object e) 
    {
    ensureCapacity();
    elements[size++] = e;
    }

    public Object pop() 
    {
    if (size == 0) 
    {
    throw new EmptyStackException();
    }
    return elements[--size];
    }

     private void ensureCapacity() 
     {
      if (elements.length == size) 
      {
      Object[] oldElements = elements;
      elements = new Object[2 * elements.length + 1];
      System.arraycopy(oldElements, 0, elements, 0, size);
      }
     }
    }
      

  7.   

    hoho怎么我的名字变成红色的了
      

  8.   

    Outof Memory只能说明你的使用内存增加太快,在JVM启动垃圾收集并生效之前已经耗尽内存。不能归咎于JAVA,内存增加这么快本身就不对。