什么也正是在这种情况下,内存泄漏?

编程入门 行业动态 更新时间:2024-10-12 05:48:20
本文介绍了什么也正是在这种情况下,内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

什么也正是在这种情况下,内存泄漏?

本文发布于:2023-11-15 09:32:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这种情况下   也正   内存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!