确定了Android GC运行时 [英] Determine when the Android GC runs

查看:195
本文介绍了确定了Android GC运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否有一种方法来识别(在code,不是LogCat中)当GC已运行?也许是一个意图解雇?我可以分析LogCat中输出,但它是理想的,如果我可以决定当GC已经从我的code运行。

Does anyone know if there is a way to identify (in code, not LogCat) when the GC has run? Perhaps an intent is fired? I could analyze the LogCat output, but it would be ideal if I could determine when the GC has run from my code.

推荐答案

您可以用弱引用的方法来做到这点:

You can do this with a weak reference trick:

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());
    }
}

现在......是否真的是正确的应用程序要做到这一点,是另一回事。 :)我知道的任何地方的唯一的地方,我们在Android中这样做是code在这里,它的唯一目的是帮助我们聪明,当我们问流程,明确GC比如当他们进入后台。

Now... whether it is really correct for an application to do this, is another thing. :) The only place I know of anywhere we have done this in Android is the code here, and its sole purpose is to help us be smart about when we ask processes to explicitly GC such as when they go into the background.

这篇关于确定了Android GC运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆