title: StrictMode activity instance count violation (2 instances, 1 expected) on rotation of completely empty activity
body: <h2>Relevant only in that it motivates eliminating any false positives from strict mode, since the continued presence of any makes the death penalty impractical</h2>
  
  <p>Over the last few days I've been chasing down and fixing memory leaks in an application. On getting to the point that I believed I'd fixed them all, I implemented a fail-loud mechanism similar to that described in <a href="https://stackoverflow.com/questions/15292383/android-strictmode-and-heap-dumps">Android StrictMode and heap dumps</a> (enable instance tracking with death penalty, intercept the shutdown error message, dump heap, send a distress signal next time application starts). All just in debug builds of course.</p>
  
  <h2>To the point</h2>
  
  <p>Having believed I've fixed all activity leaks, certain activities still result in strict mode instance violation warnings on screen rotation. Curiously it is only some, not all of the application's activities that do this.</p>
  
  <p>I've reviewed heap dumps taken when such violations occur, and reviewed the code of the activities in question in search of the leak, but do not get anywhere.</p>
  
  <p>So at this point I tried to make the smallest possible test case. I create a completely blank activity (no layout even), looking like this:</p>
  
  <pre><code>package com.example.app;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.StrictMode;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          StrictMode.setVmPolicy(
                  new StrictMode.VmPolicy.Builder()
                          .detectAll()
                          .penaltyLog()
                          .build());
          StrictMode.setThreadPolicy(
                  new StrictMode.ThreadPolicy.Builder()
                          .detectAll()
                          .penaltyDeath()
                          .penaltyLog()
                          .build());
          super.onCreate(savedInstanceState);
      }
  
  }
  </code></pre>
  
  <p>And for completeness, the manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.app" &gt;
  
      &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name="com.example.app.MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I open the activity (device held portrait). I rotate to landscape, and back to portrait, at which point I see from StrictMode this in LogCat:</p>
  
  <blockquote>
    <p>01-15 17:24:23.248: E/StrictMode(13867): class com.example.app.MainActivity; instances=2; limit=1
    01-15 17:24:23.248: E/StrictMode(13867): android.os.StrictMode$InstanceCountViolation: class com.example.app.MainActivity; instances=2; limit=1
    01-15 17:24:23.248: E/StrictMode(13867):  at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)</p>
  </blockquote>
  
  <h2>Heap dump</h2>
  
  <p>At this point I manually acquire a heap dump using DDMS. Here are the two instances in MAT:</p>
  
  <p><img src="https://i.stack.imgur.com/RaYv2.png" alt="1"></p>
  
  <p>And here's the one that was leaked, with the first part of the path from it to a GC root:</p>
  
  <p><img src="https://i.stack.imgur.com/riNHL.png" alt="2"></p>
  
  <p>Note that no matter how many times I rotate between portrait and landscape, the number of actual instances never exceeds two, and the number of expected instances never exceeds one.</p>
  
  <p>Can anyone make sense of the leak? Is it even a real leak? If it is, it presumably must be an Android bug. If it is not, the only other thing I can see that it could be is a bug in StrictMode.</p>
  
  <h2>Good answers might include</h2>
  
  <ol>
  <li>If this is a real leak, an explanation of how it occurs, and what if any action can be taken to fix it or suppress StrictMode from initiating the death penalty for such cases (recall that false positives/neutrals make the death penalty impractical).</li>
  <li>If this is not a real leak, an explanation of why StrictMode thinks otherwise, and what if any action can be taken to fix it or suppress StrictMode from initiating the death penalty for such cases (recall that false positives/neutrals make the death penalty impractical).</li>
  <li>In either case, hypotheses as to why this does not occur with all activities (the majority of activities in the application I'm working on do not produce such violations on rotation).</li>
  </ol>
  
  <p>I've got as far as looking at the StrictMode source and see nothing obviously wrong there - as expected, it forces a GC before considering an extra instance to be a violation.</p>
  
  <p>Good entry points for reading the StrictMode source are:</p>
  
  <ol>
  <li><a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.trackActivity%28java.lang.Object%29" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.trackActivity%28java.lang.Object%29</a></li>
  <li><a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.InstanceTracker.finalize%28%29" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.InstanceTracker.finalize%28%29</a></li>
  <li><a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.incrementExpectedActivityCount%28java.lang.Class%29" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.incrementExpectedActivityCount%28java.lang.Class%29</a></li>
  <li><a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.decrementExpectedActivityCount(java.lang.Class)" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/StrictMode.java#StrictMode.decrementExpectedActivityCount(java.lang.Class)</a></li>
  </ol>
  
  <h2>Full disclosure</h2>
  
  <p>I've only done these tests on one device, a Galaxy S4 running CyanogenMod 11 snapshot M2 (<a href="http://download.cyanogenmod.org/get/jenkins/53833/cm-11-20140104-SNAPSHOT-M2-jfltexx.zip" rel="nofollow noreferrer">http://download.cyanogenmod.org/get/jenkins/53833/cm-11-20140104-SNAPSHOT-M2-jfltexx.zip</a>), but I can't imagine CyanogenMod would deviate from KitKat in terms of activity management.</p>
  
  <h2>Additional StrictMode sources:</h2>
  
  <ol>
  <li>Activity's instanceTracker instance is just a final instance field: <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/app/Activity.java#Activity.0mInstanceTracker" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/app/Activity.java#Activity.0mInstanceTracker</a></li>
  <li>Where expected activity instance counts are incremented: <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/app/ActivityThread.java#2095" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/app/ActivityThread.java#2095</a></li>
  <li>Where expected activity instance counts are decremented: <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/app/ActivityThread.java#3485" rel="nofollow noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/app/ActivityThread.java#3485</a></li>
  </ol>