java - 通知中的大量内存使用

标签 java android service notifications

我正在开发一个带有服务的应用程序,该服务在通知区域(带有进度条和文本)中显示计时器的进度。我在下面提取了一个具有相同问题的更简单的示例。

服务代码:

public class TNService extends Service {
    private NotificationManager nm;
    private Notification notification;
    private RemoteViews remoteView;

    @Override
    public void onCreate () {
        nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notification = new Notification(android.R.drawable.stat_sys_download, 
                "My notification", 
                System.currentTimeMillis());
        remoteView = new RemoteViews(this.getPackageName(),
                R.layout.notification);
        remoteView.setImageViewResource(R.id.icon, android.R.drawable.stat_sys_download);
        remoteView.setTextViewText(R.id.text, "");
        remoteView.setProgressBar(R.id.progress, 100, 0, false);
        notification.flags = Notification.FLAG_NO_CLEAR;
        notification.contentView = remoteView;
        notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                TNActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

        Timer timer = new Timer ();
        timer.schedule(new TNTask(this), 0, 200);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    public void updateNotification(int progress) {
        remoteView.setProgressBar(R.id.progress, 1000, progress, false);
        remoteView.setTextViewText(R.id.text, "Progress: " + progress);
        nm.notify(0, notification);
    }
}

TimerTask的代码:

public class TNTask extends TimerTask {
    private TNService service;
    private int progress;

    public TNTask(TNService s) {
        this.service = s;
        this.progress = 0;
    }

    @Override
    public void run() {
            progress = (progress + 1) % 1000;
        this.service.updateNotification (progress);
    }
}

问题是大量的内存使用。这是 logcat 输出:

D/dalvikvm(11985): GC_EXPLICIT freed 1258 objects / 84016 bytes in 1157ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 52216 objects / 1900968 bytes in 130ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 49465 objects / 1805248 bytes in 125ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 53106 objects / 1909992 bytes in 134ms
D/dalvikvm(12008): GC_EXPLICIT freed 1604 objects / 100944 bytes in 90ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 53011 objects / 1937160 bytes in 135ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 49806 objects / 1817992 bytes in 143ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 49016 objects / 1769536 bytes in 135ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 53509 objects / 1941064 bytes in 145ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 49895 objects / 1842312 bytes in 146ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 48728 objects / 1774496 bytes in 150ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 47557 objects / 1701976 bytes in 146ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 53540 objects / 1903808 bytes in 156ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 48997 objects / 1784048 bytes in 158ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 48326 objects / 1776864 bytes in 158ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 47566 objects / 1742488 bytes in 169ms
D/dalvikvm(   85): GC_FOR_MALLOC freed 47606 objects / 1703416 bytes in 170ms
D/dalvikvm(  162): GC_EXPLICIT freed 11238 objects / 641368 bytes in 1064ms

我认为这是内存太多,过了一会儿电话挂起并显示以下输出:

D/dalvikvm(   85): GC_FOR_MALLOC freed 0 objects / 0 bytes in 241ms
I/dalvikvm-heap(   85): Clamp target GC heap from 24.008MB to 24.000MB
I/dalvikvm-heap(   85): Grow heap (frag case) to 24.000MB for 52-byte allocation
I/dalvikvm-heap(   85): Clamp target GC heap from 26.008MB to 24.000MB
D/dalvikvm(   85): GC_FOR_MALLOC freed 0 objects / 0 bytes in 241ms
I/dalvikvm-heap(   85): Clamp target GC heap from 24.008MB to 24.000MB
I/dalvikvm-heap(   85): Grow heap (frag case) to 24.000MB for 24-byte allocation
I/dalvikvm-heap(   85): Clamp target GC heap from 26.008MB to 24.000MB
D/dalvikvm(   85): GC_FOR_MALLOC freed 0 objects / 0 bytes in 247ms
I/dalvikvm-heap(   85): Clamp target GC heap from 24.009MB to 24.000MB
I/dalvikvm-heap(   85): Grow heap (frag case) to 24.000MB for 28-byte allocation
I/dalvikvm-heap(   85): Clamp target GC heap from 26.009MB to 24.000MB
D/dalvikvm(   85): GC_FOR_MALLOC freed 0 objects / 0 bytes in 247ms
I/dalvikvm-heap(   85): Clamp target GC heap from 24.009MB to 24.000MB

任何人都知道如何在不使用这么多内存的情况下做到这一点?

谢谢!

最佳答案

我偶然发现了同样的问题......似乎如果我不在服务中“缓存”RemoteView 和 Notification,而是在“更新”例程中从头开始重新创建它们,这个问题就会消失。是的,我知道它效率不高,但至少手机不会在 10-15 分钟后重启,因为内存不足。

关于java - 通知中的大量内存使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3608684/

相关文章:

java - 设计问题: to what extent should I rely on exceptions for the flow of control?

java - 在 Java 中处理循环事件的优雅方式?

java - Spring MVC HTTP 状态 400 -

android - Android 中的 values-sw 资源问题

javascript - 在 javascript 中使用网络天气服务

cocoa - 如何使 NSService 上下文菜单自动启用,而不在系统首选项-键盘-服务中选择它然后注销?

java - 如何检查 double 值是否包含特殊字符

java - CAPTURE_AUDIO_OUTPUT 在运行时未请求许可

android - 使用 "@style/Theme"后我的主题发生了变化

android - 广播接收器未注册