title: robolectric memory leak due to numeorus ReceiverDispatcher and Activity instances
body: <p>I'm writing unit tests for my company's project. Recently, our Jenkins builds started failing during unit test gradle task with <code>OutOfMemory</code> exceptions (caused by either <code>GC overhead</code> limit exceeded or <code>Java heap space</code>). On my local machine unit testing suceeds, but sometimes uses even 3.5 GB of memory. There are over 1000 tests in the project, hundreds of them create <code>Activity</code> instance.</p>
  
  <p>I've took a heap dump (in a moment when about 1000 tests passed) and examined it with VisualVM. As both memory usage and heap size are constantly rising, I suspect there's a memory leak occurring somewhere.</p>
  
  <p>I've run Eclipse Memory Analyzer with heap dump and #1 leak suspect are very numerous <code>ReceiverDispatcher</code> instances, about 255k of them (using > 500 MB of memory). Also, heap contains the same number of <code>IntentReceiverLeaked</code> instances, slightly more (258k) <code>shadowBroadcastReceiver</code> instances, and twice as less (about 127k) <code>AccessibilityManagerService</code> and <code>LockPatternUtils</code> instances.</p>
  
  <p>This looks strange to me, because I found only one <code>BroadcastReceiver</code> being registered dynamically in the project (in <code>MainActivity</code>), and it's being properly unregistered in Activity's <code>onDestroy()</code> method.</p>
  
  <p>Since there are about 300 Activity instances in the dump, I suspect leaking Activities as a main cause of the leak. Also, Eclipse Memory Analyzer lists <code>ShadowContextImpl</code> as a #2 problem suspect, with slightly more instances than Activities (but about 500 MB of memory used).</p>
  
  <p>Activities in tests are created using <code>Robolectric.setupActivity()</code>, and relevant lifecycle methods are called on <code>tearDown()</code></p>
  
  <pre><code>//Android Annotations are in use
  protected ActivityController&lt;MainActivity_&gt; activityController;
  protected MainActivity_ mainActivity;
  
  @Before
  public void setUp() throws Exception {
      activityController = Robolectric.buildActivity(MainActivity_.class).setup();
      mainActivity = activityController.get();
  }
  
  @After
  public void tearDown() throws Exception {
      try {
          if (activityController != null) {
              activityController.pause()
                      .stop()
                      .destroy();
          }
      } catch (Throwable th){}
  
      mainActivity = null;
      activityController = null;
  }
  </code></pre>
  
  <p>For Fragment-related tests, Fragment is created and attached to Activity in a regular way (instead of using FragmentTestUtil)</p>
  
  <pre><code>public static void startFragment(Fragment fragment, FragmentActivity activity) {
      shadowOf(Looper.getMainLooper()).pause();
      FragmentManager fragmentManager = activity.getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(fragment, null);
      fragmentTransaction.commitAllowingStateLoss();
      shadowOf(Looper.getMainLooper()).runToEndOfTasks();
  }
  
  (...)    
  Activity activity = Robolectric.setupActivity(MainActivity_.class);
  MyFragment fragment = MyFragment_.builder().build();
  startFragment(fragment, activity);
  (...) 
  </code></pre>
  
  <p><strong>What could be the cause of those leaks (and how to fix it) ? Could the way Activities and Fragments are initiated have something to do with the leak ?</strong></p>