问题描述
我记得有人告诉我一个好的。但我不记得它。我花了最后20分钟与谷歌试图了解更多。什么是坏/不伟大的代码的例子,导致由于垃圾收集性能打击?
来自旧的 以这种方式重写pop方法有助于确保及时完成垃圾回收时尚: I remembered someone telling me one good one. But i cannot remember it. I spent the last 20mins with google trying to learn more. What are examples of bad/not great code that causes a performance hit due to garbage collection ? from an old sun tech tip -- sometimes it helps to explicitly nullify references in order to make them eligible for garbage collection earlier: rewriting the pop method in this way helps ensure that garbage collection gets done in a timely fashion: 这篇关于垃圾收集瓶颈的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
public class Stack {
private static final int MAXLEN = 10;
private Object stk [] = new Object [MAXLEN];
private int stkp = -1;
public void push(Object p){stk [++ stkp] = p;}
public Object pop(){return stk [stkp--];}
public Object pop(){
Object p = stk [stkp];
stk [stkp--] = null;
return p;
}
public class Stack {
private static final int MAXLEN = 10;
private Object stk[] = new Object[MAXLEN];
private int stkp = -1;
public void push(Object p) {stk[++stkp] = p;}
public Object pop() {return stk[stkp--];}
}
public Object pop() {
Object p = stk[stkp];
stk[stkp--] = null;
return p;
}