本文介绍了垃圾收集瓶颈的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我记得有人告诉我一个好的。但我不记得它。我花了最后20分钟与谷歌试图了解更多。



什么是坏/不伟大的代码的例子,导致由于垃圾收集性能打击?

解决方案

来自旧的

  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--];}

以这种方式重写pop方法有助于确保及时完成垃圾回收时尚:

  public Object pop(){
Object p = stk [stkp];
stk [stkp--] = null;
return p;
}


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:

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--];}
}

rewriting the pop method in this way helps ensure that garbage collection gets done in a timely fashion:

public Object pop() {
  Object p = stk[stkp];
  stk[stkp--] = null;
  return p;
}

这篇关于垃圾收集瓶颈的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:55