如何衡量Android应用程序数据大小并确定存储泄漏? [英] How to measure Android app data size and identify storage leaks?

查看:108
本文介绍了如何衡量Android应用程序数据大小并确定存储泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个小型Android应用程序,并且已经使用了一段时间。我注意到,在设置中,我的应用程序特征的数据行(我对分析应用程序行或缓存行中显示的数量不感兴趣)显示了大约20 MB,对我来说似乎很多。恐怕该应用存在存储泄漏(即,它产生的数据从未被擦除)。

I made a small Android app and have been using it for a while now. I noticed that, in the settings, on the "data" line of my app's characteristics (I am not interested in analysing the amount shown on the "application" line nor on the "cache" line), it shows about 20 MB, which seemed a lot to me. I am afraid the app has storage leaks (i.e. that it produces data that is never erased).

我决定调查并衡量可能占用那么多空间的内容。我为此应用程序使用了所有可用的存储选项 :SQLite DB,内部文件,外部文件,共享首选项(以及包括 Glide 图片加载的缓存文件)。

I decided to investigate and measure what might take that much space. I use pretty much all the available storage options for this app: SQLite DB, internal files, external files, shared preferences (and cache files including Glide picture loading).

到目前为止,感谢有关SQLite DB的问题,我发现我的数据库文件大约需要500 kB。我通过递归扫描文件夹 getFilesDir()中的文件和文件夹发现,我在内部文件和应用程序专用外部文件中使用了10 kB的数据。我还没有分析共享首选项的大小,但是我存储的键/值对少于20个。

So far, thanks to a question on SQLite DB, I found that my DB file takes about 500 kB. I found by scanning recursively the files and folders in the folder getFilesDir() that I use 10 kB of data in internal and app-private external files. I have not analysed Shared Preferences size yet, but I store less than 20 key/value pairs.

浏览文件夹 getCacheDirs()我还发现Glide使用了大约3 MB的缓存(接近Android设置应用程序告诉的内容)。

Exploring the folder getCacheDirs() I also found that Glide uses about 3 MB of cache (close to what the Android settings app tells).

我的问题是,我错过了哪些线索找不到我无法找到的19.5 MB数据的位置?我是否忘了某种可能占用空间的存储空间?而且,更一般而言,是否有工具来分析存储泄漏(即,应用程序产生的数据可能永远不会擦除)?

My question is, which leads did I miss to find where this 19.5 MB of data I cannot locate? Did I forget some kind of storage that might take space? And, more generally, are there tools to analyse storage leaks (i.e. data produced by the app that might never be erased)?

推荐答案

按照@James的建议,我开始浏览不同的文件夹,在与同事讨论问题并尝试浏览许多文件夹之后,我终于发现大部分数据来自我以前拥有的Webview的缓存。我正在发布所有调查信息,希望对您有所帮助。

Following @James suggestion, I started exploring the different folders, and after discussing the issue with a colleague and trying to explore many folders, I finally found that most of the data came from the cache of a former Webview I had. I am posting all my investigation, hopefully it will help.

在应用启动时,我执行了以下代码,调用了 analyseStorage(this)来自我的主要活动:

I executed the following code when the app starts, calling analyseStorage(this) from my main activity:

public void analyseStorage(Context context) {
  File appBaseFolder = context.getFilesDir().getParentFile();
  long totalSize = browseFiles(appBaseFolder);
  Log.d(STORAGE_TAG, "App uses " + totalSize + " total bytes");
}

private long browseFiles(File dir) {
  long dirSize = 0;
  for (File f: dir.listFiles()) {
    dirSize += f.length();
    Log.d(STORAGE_TAG, dir.getAbsolutePath() + "/" + f.getName() + " uses " + f.length() + " bytes");
    if (f.isDirectory()) {
      dirSize += browseFiles(f);
    }
  }
  Log.d(STORAGE_TAG, dir.getAbsolutePath() + " uses " + dirSize + " bytes");
  return dirSize;
}

重要的是要专门扫描 context.getFilesDir ().getParentFile()与文件夹 /data/data/my.app.package /

What is important is to scan specifically context.getFilesDir().getParentFile() which matches the folder /data/data/my.app.package/

执行该代码后,我有以下日志:

After executing that code, I had the following logs:

D/storage﹕ /data/data/my.app.package/lib uses 0 bytes
D/storage﹕ /data/data/my.app.package/cache uses 3371773 bytes
D/storage﹕ /data/data/my.app.package/databases uses 483960 bytes
D/storage﹕ /data/data/my.app.package/shared_prefs uses 604 bytes
D/storage﹕ /data/data/my.app.package/app_webview uses 9139469 bytes
D/storage﹕ /data/data/my.app.package/files uses 7723 bytes
D/storage﹕ /data/data/my.app.package/app_ACRA-approved uses 0 bytes
D/storage﹕ /data/data/my.app.package/app_ACRA-unapproved uses 0 bytes
D/storage﹕ App uses 13003529 total bytes

我可以看到的是:


  • 仅使用缓存由Gli de用于图片加载,需要3MB

  • SQLite数据库需要500kB

  • 共享首选项需要600B

  • 我以前拥有的所有Webview的缓存仍然需要9MB

  • 文件和其他文件夹下的其余文件是最终,我最终发现我的大部分数据都存储在Webview缓存中了,通常由ACRA用来进行错误跟踪并采取10kB

  • The cache, used only by Glide for picture loading, takes 3MB
  • The SQLite database takes 500kB
  • The shared preferences take 600B
  • The cache for all the Webviews I used to have still takes 9MB
  • The rest of the files, under files and other folders, is mostly used by ACRA for bug tracking and take 10kB

实际上没有显式存储为缓存。我删除了这些文件,实际上将应用程序的大小减少了20MB,甚至超过了上面列出的大小。现在,我知道我的应用程序数据要经过的数量级。

In the end, I finally discovered that most of my data went to Webview cache, actually not stored explicitly as cache. I deleted these files and it actually reduced the size of my app by 20MB, even more than listed above. I now know what order of magnitude my app's data takes.

这篇关于如何衡量Android应用程序数据大小并确定存储泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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