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)?
You can use a library like Leak Canary to automatically detect all memory leaks inside your app: https://corner.squareup.com/2015/05/leak-canary.html
You can also go to the DDMS in Android Studio and get more information of the storage in your app without coding anything. However, most of the leaks would be detected by Leak Canary.
Hope it helps!
There is no storage leaks.
You didn't count
odex
(dalvik) oroat
(android runtime) file in. They are usually located atThese files are generated by system for optimization during installing time.
Also you didn't count your APK file in which located at
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
So the storage size you counted under
/data/data/xxx.yyy.zzz
is always less than the total size in settings.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: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:
What I could see is:
files
and other folders, is mostly used by ACRA for bug tracking and take 10kBIn 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.
You can use MAT TOOL for optimizing memory/size issues in your app.It displays which part in your app using more memory.
Run you app in Android-studio and Go to Tools->Android->AndroidDeviceMonitor,run your scenario and in device monitor click to download your app .hprof file by follow the image I attached below
After that you need to convert Android-studio .hprof file to Eclipse Mat supported .hprof file by using hprof-conv.exe inside sdk->platform-tools and follow the cmd in your CMD promt
And in MAT Tool go to File -> OpenHeapDump to open your .hprof file,It will show your app memory utilize by package,class,object..etc;
Some referral link for learn about MAT Tool
link 1
link 2