Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I want to find "Duplicate Strings" with HPROF Viewer and Analyzer?

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?

like image 745
kalabalik Avatar asked Jun 30 '16 09:06

kalabalik


People also ask

How long does it take for heap dump?

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.

How do you analyze a memory leak heap dump?

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.


1 Answers

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.

like image 126
SGill Avatar answered Oct 21 '22 16:10

SGill