java Android缓存后台进程不断增加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29193726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:51:20  来源:igfitidea点击:

Android cache background process is increasing continuously

javaandroidcachingmemory-managementmemory-leaks

提问by Manish Agrawal

In my android application named "PriceDekho" the cache background process takes too much space. First when i start the application the application takes arround 30MB (I think its OK) but the problem is when i surfing the application pages then this size is increasing continuously upto (200 MB). This appliction "Cache background process" also varying with the mobile RAM size. If the mobile RAM size is 2GB then this application "Cache background process" size goes upto 500 to 700 MB.

在我名为“PriceDekho”的 android 应用程序中,缓存后台进程占用了太多空间。首先,当我启动应用程序时,应用程序占用了大约 30MB(我认为还可以),但问题是当我浏览应用程序页面时,这个大小不断增加到(200 MB)。此应用程序“缓存后台进程”也随移动 RAM 大小而变化。如果移动 RAM 大小为 2GB,则此应用程序“缓存后台进程”大小将达到 500 到 700 MB。

My application is having only 5 to 6 screens. I just need to stabilize the cached background size.

我的应用程序只有 5 到 6 个屏幕。我只需要稳定缓存的背景大小。

How can i clear the application cached size? Please help.

如何清除应用程序缓存大小?请帮忙。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Murat Karag?z

It sounds like you have memory leaks which the Garbage Collector can not remove. For example if you need to have a reference to the Contextfrom a non-context class which is never released there. Most of the time Android Studio will point those out but you can try to use LeakCanaryand search for them.

听起来您有垃圾收集器无法删除的内存泄漏。例如,如果您需要引用Context从未在那里发布的非上下文类。大多数情况下,Android Studio 会指出这些,但您可以尝试使用LeakCanary并搜索它们。

Add the dependency

添加依赖

dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
 }

and use it in your Applicationclass. Create one if you don't have it already.

并在你的Application课堂上使用它。如果您还没有,请创建一个。

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {
      // This process is dedicated to LeakCanary for heap analysis.
      // You should not init your app in this process.
      return;
    }
    LeakCanary.install(this);
    // Normal app init code...
  }
}

回答by Sumeet

Cache background process surely varies with the RAM size as the system will allow more data to be cached if more memory is available.

缓存后台进程肯定会随 RAM 大小而变化,因为如果有更多可用内存,系统将允许缓存更多数据。

However, the increasing pattern in memory usage by the application may either be due to memory leaks or huge objects being created in the background. The Garbage collector is not an all weather friend and may tend to ignore memory leaks due to programmatic mistakes.

但是,应用程序内存使用量的增加模式可能是由于内存泄漏或在后台创建了巨大的对象。垃圾收集器不是全天候的朋友,并且可能倾向于忽略由于程序错误导致的内存泄漏。

As there is no code posted for review, some trivial questions to ask would be:

由于没有发布供的代码,所以要问的一些琐碎问题是:

  • Is there a lot of image processing being done using bitmaps? If yes, are those bitmaps being recycled ?
  • Is your application using the context judiciously, that is avoiding usage of the application context, unless required ?
  • Are the listeners being unregistered, if any ?

    As suggested above, LeakCanary will surely be much useful in this case, surely more than the Android Monitor.

  • 是否有大量使用位图进行的图像处理?如果是,这些位图是否被回收?
  • 您的应用程序是否明智地使用上下文,即避免使用应用程序上下文,除非需要?
  • 听众是否未注册(如果有)?

    如上所述,LeakCanary 在这种情况下肯定会非常有用,肯定比 Android Monitor 更有用。