title: Swift Managing Memory
body: <p>This question was cleaned up and the important info moved to the answer below.</p>
  
  <hr>
  
  <p>I have some questions about memory management.</p>
  
  <p>I am building a photo editing app. So keeping memory usage low is important.
  Also I am not going to post code because I do not have a big memory leak when doing one specific thing. I just lose a couple of KB's/MB's with everything that happens. And going over tens of thousands of lines of code to find kilobytes is no fun ;)</p>
  
  <p>my app uses core data, lots of cifilter stuff, location, and the basics.</p>
  
  <p>My first view is just a tableview which costs me about 5mb of memory.
  Then you take some photos, apply some filters, this gets saved to core data and then you go back to that first view. </p>
  
  <p><strong>Is it possible to truly get rid of everything in memory except for the data needed to drive that first view. (that very save and awesome 5mb)</strong> </p>
  
  <p>Or will there always be something left behind, even if you set everything to nil?</p>
  
  <hr>
  
  <p><strong>Bonus Question:</strong>
  is there a difference in file size / cpu load between <code>UIImageJPEGRepresentation</code> and <code>UIImagePNGRepresentation</code>?
  I know you can set a compression quality with the JPEG method (harder on the cpu/gpu?). </p>
  
  <p>Just trying to reduce memory pressure by all means possible.</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>It was pointed out to me that the question might be too vague.</p>
  
  <p>The problems I was having at some point or another were the following:</p>
  
  <ul>
  <li>At some points peak memory usage is too high</li>
  <li>Navigating to a second viewcontroller and back causes a leak</li>
  <li>Editing an image causes a memory leak.</li>
  <li>Applying a filter to more than 4-5 images causes a crash because of low memory, there were no more memory leaks at this point. (verified in instruments) </li>
  </ul>
  
  <hr>
  
  <p>P.s this was all tested on an iPhone 4s , not the simulator.</p>
  
  <p><em>There was a meme here to lighten the mood on this site a bit.</em> </p>