Using the Android Memory Monitor, especially its "Dump Java Heap" feature, I am currently hunting down memory leaks in my app. On the left of Android Studio's dump/HPROF view there is an option to open "Analyzer Tasks" and there you can tell the machine to "Detect Leaked Activities" and to "Find Duplicate Strings". I am puzzled by the latter option. What does it do and how is it useful? It must be more sophisticated than just helping those clumsy developers among us who put the same string twice into their resources folder. The docs are not very helpful here (if my search was thorough enough) as they only state that it helps in cases "where the target program has strings that repeat values". When would this be the case?
Taking a heap dump pauses the running JVM for a relatively brief period. Generating a dump takes about 2 sec per 1 GB of used heap. So if, for example, your app uses 4 GB, it would be stopped for 8 seconds.
Using JMAT Tool to Analyze Heap Dump You can Scroll down under Overview tab and then click on Leak Suspects to find the details as shown in below screenshots to pinpoint the class responsible for OutOfMemoryError and the number of Objects that was created.
As far as I know, this does just point out duplicated strings in memory. However, this is useful for more than just finding cases where the same string has been entered into more than one resource. For example, as Strings are immutable in Java, you can easily end up with many more instances of strings than you might initially realise. If your app has lots of string concatenation code but dont use StringBuilder, or if your app does any string/text processing, its fairly easy to end up with unexpectedly large amounts of space taken up by strings. But conversely, it can often be a relatively easy optimization to make in order to gain back some space (once you can see what the problem is). So you could say this task is more about memory 'optimization', rather than finding a leak.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With