title: How to avoid memory leak in context.getSystemService(Context.CAMERA_SERVICE)?
body: <p>I've found a memory leak in Android 5.x Camera2 API which I also <a href="https://code.google.com/p/android/issues/detail?id=182416&amp;thanks=182416&amp;ts=1439298992" rel="nofollow noreferrer">reported</a>. The problem is when you use Android Lollipop device that has Camera2 API implemented in <code>LEGACY</code> mode. On such devices, calling <code>context.getSystemService(Context.CAMERA_SERVICE)</code> causes <code>context</code> to be retained so that it won't be garbage collected.</p>
  
  <p>If this <code>context</code> is your activity that is started multiple times, you can end up with hanging references to dozens of instances of your activity that are never garbage collected.</p>
  
  <p>The issue appears to happen only on Lollipop devices that have Camera2 API implemented in <code>LEGACY</code> mode (e.g. HTC One M8, Samsung Galaxy S4), while it does not happen on Samsung Galaxy S6 which implements Camera2 API in <code>FULL</code> mode.</p>
  
  <p>To demonstrate the issue, I've created a <a href="https://app.box.com/s/uijyleeb6zic76a49umjqjyq9pcwzfhd" rel="nofollow noreferrer">small demo app</a>. The app contains two activities: first that contains a button which calls the second activity. The second activity obtains the <code>CameraManager</code> and queries the level of Camera2 API support for first back-facing camera and returns the result to first activity. If you run the app on device that implements Camera2 API in <code>LEGACY</code> mode, after tapping the button 98 times, causing GC and then dumping HPROF you will see exactly 98 live instances of <code>Main2Activity</code>, like this <a href="http://www.pohrani.com/f/1H/gs/4EFlHKoj/sgs4.png" rel="nofollow noreferrer">http://www.pohrani.com/f/1H/gs/4EFlHKoj/sgs4.png</a></p>
  
  <p>If you do the same on device that implements Camera2 API in <code>FULL</code> mode, you will see 0 live instances of <code>Main2Activity</code>, like this <a href="http://www.pohrani.com/f/2q/bV/4srUZIJL/sgs6.png" rel="nofollow noreferrer">http://www.pohrani.com/f/2q/bV/4srUZIJL/sgs6.png</a></p>
  
  <p>Is there a way to workaround this leak?</p>
  
  <p>One might ask why am I doing this? At our company, we are developing <a href="https://microblink.com/" rel="nofollow noreferrer">barcode and OCR scanning solutions</a> and also a famous <a href="https://photomath.net/en/" rel="nofollow noreferrer">PhotoMath app</a>. So we have a scan activity that controls the camera and scanning process. While starting up, activity checks if device supports Camera2 API in either <code>FULL</code> or <code>LIMITED</code> mode and attempts to use it for better performance, whilst if Camera2 API is in <code>LEGACY</code> mode, then we prefer using camera management using old camera API, as we do on pre-Lollipop devices. </p>
  
  <p>Because of the mentioned memory leak, whenever a client that has integrated our SDK into its app starts the scan activity, performs a scan and obtains the result, one instance of scan activity will be leaked due to the bug. If client scans a lot, this can eat up a more than 20 MB of memory - a serious issue!</p>
  
  <p>So if someone knows how to make a workaround for this issue, I'll be eternally grateful!</p>