0

我有一个动态壁纸应用程序,它使用从网上下载的照片用作壁纸。

内存管理器显示我的应用程序使用了 90MB 到 120MB(它是一个具有 1GB RAM 的设备)。那是很多。壁纸服务经常被杀死,有时系统甚至会恢复为默认的静态壁纸。

尽管我彻底检查了是否存在任何内存泄漏,并且 HPROF 转储分析表明我只有 2 个大对象在使用:当前用于墙纸的位图(16MB - 这是预期的 4Mpix 图像)和类 android.content.res.Resource( 12MB),HPROF 概览屏幕中报告的总大小为 32MB。

dumpsys meminfo 显示:

** MEMINFO in pid 1354 [com.myapp.lwp] **
                     Shared  Private     Heap     Heap     Heap
               Pss    Dirty    Dirty     Size    Alloc     Free
            ------   ------   ------   ------   ------   ------
   Native       16       16       16   112304     5274      993
   Dalvik    41947    19316    41428    74572    53120    21452
   Cursor        0        0        0
   Ashmem        0        0        0
Other dev    20170    33176     3460
 .so mmap     5690     2620     1504
 .jar mmap        0        0        0
 .apk mmap       64        0        0
 .ttf mmap        6        0        0
 .dex mmap      604        0        4
Other mmap     1035      300      272
  Unknown     5127      472     5116
    TOTAL    74659    55900    51800   186876    58394    22445

Objects
           Views:       27         ViewRootImpl:        0
     AppContexts:        4           Activities:        1
          Assets:        3        AssetManagers:        3
   Local Binders:       16        Proxy Binders:       23
Death Recipients:        0
 OpenSSL Sockets:        1

我读到:如何在 Android 中发现我的应用程序的内存使用情况?但这并不能帮助我得出任何结论...

我也用过:

    cat /proc/1354/statm
    139422 29326 11550 2 0 10330 0

它也没有多大帮助。

DDMS 显示 63MB 堆大小。

我的问题是?处理双倍屏幕尺寸图像的应用程序是否正常和预期?如果没有,剩下的 60-90MB 是什么?(我总共减去了 HPROF 给我的 30MB)。在我的应用程序中使用这么多内存可能会做错什么?什么应用程序实际做:

1) Download list of photos (max 100) as JSON string and parse that string to get ids and urls for these photos.
2) Download first photo and save it to cache folder
3) Decode photo and if necessary resize it to fit 2Width*Height of screen (photos are actualy smaller that my Full HD screen).
4) Draw it on wallpaper canvas (and draw repeatedly while user is scrolling)
5) If on WIFI download next 10 photos to cache folder (without decoding, just saving)

我有其他更复杂的模式,同时使用了更多照片,但以上内存使用量适用于最简单的场景。

这不是连续使用的问题 - 内存在第一次启动后描述:如果我终止服务,重新启动后它使用 90MB。在第一次打开选项屏幕后(带有几个选项的小活动/对话框可供选择 - 除了背景可绘制对象之外没有图形)内存使用量跃升至 124MB,并在屏幕关闭后返回到 112MB。第一次更换壁纸照片后,它会跳到 130+ 并返回到 120+。可能是因为堆大小在位图解码期间增加,然后保持这种状态。

我应该怎么办?如何减少(如果可能的话)我的应用程序内存占用?还有哪里可以看?我不指望现成的解决方案,但任何进一步的指导都可以帮助......

4

1 回答 1

1

I had the same problem while working on my application. The DUMP and the GC (From Logcat) were both indicating that I'm using around 30-50 MB of memory, fluctuating up and down, which is normal since the GC is running at different times.

However, for some reason it seems the process RAM usage was just building up and not getting released, especially if you have a running service. My process showed that I hit over 300 MB of RAM, so I wasn't sure whether it's just not released (GCed already) or it's being leaked.

What I did to verify is, I let it be for a while after closing the application, and in few hours it showed my service is using 3 MB of RAM only, also, I opened many applications (To force the Android to perform a GC) and again it went back from 300 MB to 3 MB without killing my service, so I assume it just kept building it up without GCing it, so it wasn't leaked.

This is still not a good enough insurance but it's better than nothing, I'm working on trying to find out more about it, hope that helps.

Kindly let me know if you ever figure out the solution to that problem, thanks.

于 2014-02-03T20:58:57.433 回答