代码之家  ›  专栏  ›  技术社区  ›  Chris Thompson

确定何时运行Android GC

  •  4
  • Chris Thompson  · 技术社区  · 15 年前

    有人知道当GC运行时是否有一种方法来标识(在代码中,而不是logcat)?可能是故意被解雇了?我可以分析logcat输出,但如果我可以确定何时从我的代码运行GC,这将是理想的选择。

    2 回复  |  直到 14 年前
        1
  •  9
  •   hackbod    15 年前

    WeakReference<GcWatcher> mGcWatcher
            = new WeakReference<GcWatcher>(new GcWatcher());
    long mLastGcTime;
    
    class GcWatcher {
        @Override
        protected void finalize() throws Throwable {
            handleGc();
            mLastGcTime = SystemClock.uptimeMillis();
            mGcWatcher = new WeakReference<GcWatcher>(new GcWatcher());
        }
    }
    

        2
  •  -3
  •   Seth    15 年前