Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure Android app data size and identify storage leaks?

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).

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).

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.

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

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)?

like image 831
Vince Avatar asked Mar 11 '16 22:03

Vince


Video Answer


2 Answers

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.

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;
}

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

What I could see is:

  • 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

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.

like image 65
Vince Avatar answered Nov 07 '22 09:11

Vince


There is no storage leaks.

You didn't count odex(dalvik) or oat(android runtime) file in. They are usually located at

/data/dalvik-cache/xxx.odex
/data/dalvik-cache/<target-architecture>/xxx.oat

These files are generated by system for optimization during installing time.

Also you didn't count your APK file in which located at

/data/app/xxx.yyy.zzz.apk

The directories or files are not accessible from adb shell if the device is not rooted.

I think the storage usage show at settings include the following three parts

/data/data/xxx.yyy.zzz
/data/app/xxx.yyy.zzz.apk
odex or oat file

So the storage size you counted under /data/data/xxx.yyy.zzz is always less than the total size in settings.

like image 27
alijandro Avatar answered Nov 07 '22 09:11

alijandro