3

I started a brand new sample project for this experiment. The app has one activity.

At this point I run the application and dump the java heap. The results are shown below:

enter image description here

Great! Everything looks correct. The "Total Count" and "Heap Count" are at 1. Exactly what I expected.

Let's rotate my device 3 times. At this point I'm still expecting to see only 1 MainActivity, but I understand that 4 have been created. Let's take a look.

enter image description here

Hmm... That's weird. It is showing 4. How could that be? Maybe the activities are still there. Unreferenced... but they haven't been GC'd yet. Let's force a GC and do another heap dump.

enter image description here

Strange. It's still at 4. This is where I'm lost. It sounds like either Total Count or Heap Count should be at 1. Is it just keeping every reference of MainActivity ever? Is that a leak? Is the viewer just wrong?

Total Count (according to the docs) is "Total number of instances outstanding."

Maybe Heap Count? "Number of instances in the selected heap." Well that sounds like more of what I want. I want the number of instances (real instances) with gc roots.

Does anyone have any idea why this is happening?

EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • You can use the heap dump explorer to see what is referencing your activity instances. – CommonsWare Aug 20 '16 at 21:12
  • http://stackoverflow.com/questions/36239142/android-app-orientation-change-restarts-activity – NickT Aug 20 '16 at 21:17
  • @CommonsWare is that another program? Am I going crazy? Or am I right in thinking that the number should only be 1 in those two columns? – EGHDK Aug 20 '16 at 23:24
  • 1
    "is that another program?" -- no, it is what you have the screenshots of above. Assuming that you still have my book, read [the chapter on memory leaks](https://commonsware.com/Android/previews/finding-memory-leaks). [Version 7.0](https://commonsware.com/blog/2015/12/15/busy-coders-guide-android-development-7p0-released.html) and higher has material on the Android Studio heap analyzer. "Am I going crazy?" -- I am unqualified to answer that question. :-) – CommonsWare Aug 20 '16 at 23:35
  • "Or am I right in thinking that the number should only be 1 in those two columns?" -- it should be. However, you are blaming the tool. It is entirely possible that there is a bug in that tool. It is also entirely possible that there is a bug in whatever the code is that you put through the tool. – CommonsWare Aug 20 '16 at 23:35
  • Thanks. I'm going to try AS Canary and see if I get the same issue. – EGHDK Aug 20 '16 at 23:40
  • I'm very surprised and disappointed that this doesn't have an answer by now. – Nerdy Bunz Aug 14 '18 at 06:50

0 Answers0