Topic: Java 内存泄漏的例子

  Print this page

1.Java 内存泄漏的例子 Copy to clipboard
Posted by: rainman
Posted on: 2002-12-09 07:21

回复本贴可以看到哪段代码有内存泄漏,另外可以用Optimizeit Profiler检测出内存泄漏来,具体方法以后介绍咯。


// Can you spot the "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];
}

/**
* Ensure space for at least one more element, roughly
* doubling the capacity each time the array needs to grow.
*/
private void ensureCapacity() {
if (elements.length == size) {
Object[] oldElements = elements;
elements = new Object[2 * elements.length + 1];
System.arraycopy(oldElements, 0, elements, 0, size);
}
}
}


[hidden segment]

2.pop时候的问题吧。 [Re: rainman] Copy to clipboard
Posted by: SimonLei
Posted on: 2002-12-09 08:22


3.看看 [Re: rainman] Copy to clipboard
Posted by: 九佰
Posted on: 2002-12-09 09:06


4.look [Re: rainman] Copy to clipboard
Posted by: scottding
Posted on: 2002-12-09 09:20


5.Re:Java 内存泄漏的例子 [Re: rainman] Copy to clipboard
Posted by: smallred
Posted on: 2002-12-09 09:33

最讨厌隐藏贴!!!!!!!!!!!!!!!!!!!!!!!!!!!

6.Re:Java 内存泄漏的例子 [Re: smallred] Copy to clipboard
Posted by: rainman
Posted on: 2002-12-09 09:35

smallred wrote:
最讨厌隐藏贴!!!!!!!!!!!!!!!!!!!!!!!!!!!

呵呵,那答案不就一下看到了吗? 晕!

7.look~ [Re: rainman] Copy to clipboard
Posted by: via
Posted on: 2002-12-09 09:46


8.Re:Java 内存泄漏的例子 [Re: rainman] Copy to clipboard
Posted by: mizi
Posted on: 2002-12-09 11:38

看看 。

9.pop的时候没处理元素吧 [Re: rainman] Copy to clipboard
Posted by: jmd7
Posted on: 2002-12-09 12:53


10.Re:Java 内存泄漏的例子 [Re: smallred] Copy to clipboard
Posted by: Biubiu
Posted on: 2002-12-09 12:57

smallred wrote:
最讨厌隐藏贴!!!!!!!!!!!!!!!!!!!!!!!!!!!


这是你的第一个帖子。恭喜。终于浮出水面了Dead

11.answer [Re: rainman] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-09 14:11

should not be a big problem though

The number of the kept references will not exceed the length of the stack. I think it will usually not lead to OutOfMemory exception.

12.Re:Java 内存泄漏的例子 [Re: rainman] Copy to clipboard
Posted by: floweret
Posted on: 2002-12-10 09:28


13.Re:Java 内存泄漏的例子 [Re: rainman] Copy to clipboard
Posted by: guoxianbin
Posted on: 2002-12-10 10:21

Let me see.

14.Re:Java 内存泄漏的例子 [Re: rainman] Copy to clipboard
Posted by: youlq
Posted on: 2002-12-10 10:55

public Object pop() {
if (size == 0) { throw new EmptyStackException(); }

return elements[--size]; //here

}

15.check [Re: rainman] Copy to clipboard
Posted by: neosun
Posted on: 2002-12-10 14:36

看一看是怎么做的。

16.回答问题正确都有加分哦 [Re: rainman] Copy to clipboard
Posted by: rainman
Posted on: 2002-12-10 14:44

呵呵,调节一下学术气氛。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923