什么也正是在这种情况下,内存泄漏? [英] What's exactly a memory leak in this case?

查看:151
本文介绍了什么也正是在这种情况下,内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要清除所有后台活动,我做了以下内容:

To clear all background activities I did the following:

我一直在一个静态数组列表,每当我去从一个活动到另一个,在OnCreate()新活动的方法,我添加了当前活动的对象变成该名单是这样的:

I kept a static array list, and whenever I used to go from one activity to another, in the onCreate() method of new activity, I added the object of current activity into that list like this:

SomeClass.addActivity(CurrentActivity.this);

我在每个活动添加上述表示的。

I added the above statement in each activity.

该addActivity():

The addActivity():

public void addActivity(final Activity activity) {
            activityList.add(activity);
        }

而当我想以清除栈,我叫:

And when I wanted to clear the stack, I called:

public boolean clearStack() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        activityList.clear();
        return (activityList.isEmpty());
    }

在这样,我明白我的活动栈。

In this way, I cleared my activity stack.

但它产生了内存泄漏。这不是这样做的正确方法。这也不行持有至活动引用。你们能解释我为什么和如何准确内存泄漏发生在这种情况下?

But it produced a memory leak. It's not the right way to do that. It's not ok to hold references to activities. Can you guys explain me why and how exactly memory leak occurred in this case?

我用MAT月食找到我的应用程序此内存泄漏。

I used MAT for eclipse to find this memory leak in my app.

任何帮助将大大AP preciated。

Any help would greatly be appreciated.

推荐答案

抱着活动引用其上下文外(当他们在背景或关闭/成品),导致内存泄漏 - Android操作系统想清楚从内存中的老的活动,当它决定它这样做的时候(你不能手动控制)。

Holding references to activities outside their context (when they are in background or "closed"/finished) causes memory to leak - the Android operating system would like to clear from memory "old" activities when it decides it's the time to do so (You can't control it manually).

在这种情况下 - 垃圾收集器会尝试释放内存中的活动/活动,但由于东西(你引用数组活动)持有对它的引用 - 它不能被垃圾收集,因此它可以'T从内存 - 中解放出来,这是一个内存泄漏的样本。

In that case - the garbage collector would try to free the activity / activities from memory, but because something (your array of references to activities) is holding a reference to it - it can't be garbage collected, so it can't free it from memory- and that's a sample of a memory leak.

本文档介绍了如何避免内存泄漏:

This document describes how to avoid memory leaks:

HTTP://android-developers.blogspot。 co.uk/2009/01/avoiding-memory-leaks.html

这篇关于什么也正是在这种情况下,内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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