title: LZMA SDK decompress for iOS (xcode) using too much RAM
body: <p>I am trying to use the LZMA SDK in an iPhone/iPad app, my starting point was the LZMA example project for iPhone provided by Mo Dejong, available here:
  <a href="https://github.com/jk/lzmaSDK" rel="nofollow noreferrer">https://github.com/jk/lzmaSDK</a>
  Original was here: <a href="http://www.modejong.com/iOS/lzmaSDK.zip" rel="nofollow noreferrer">http://www.modejong.com/iOS/lzmaSDK.zip</a>
  (I tried both and I get the same result from both).</p>
  
  <p>The problem is that the extract uses as much RAM as the .7z contains uncompressed.  In other words, say I have a 40MB compressed file, the uncompressed file is a binary sqlite DB that is about 250MB, it will slowly use up more and more memory as it uncompresses the file all the way up to 250MB.  This will crash an iPad1 or anything before iPhone4 (256MB RAM).  I have a feeling a lot of people will eventually run into this same problem, so a resolution now could help a lot of developers.</p>
  
  <p>I originally created the .7z file on a PC using windows based 7-zip (latest version) and a 16MB dictionary size.  It should only require 18MB of RAM to uncompress (and that is the case when testing on a PC looking at task manager). I also tried creating the archive using keka (the open source mac archiver), it did not resolve anything, although I can confirm that keka itself only uses 19MB of ram during its extract of the file on a mac which is what I would expect.  I guess the next step would be to compare the source code of Keka to the source code of the LZMA SDK. </p>
  
  <p>I played around with different dictionary sizes and other settings when creating the .7z file but nothing helped.  I also tried splitting my single binary file into 24 smaller pieces before compressing, but that also did not help (still uses over 250MB of RAM to extract the 24 pieces).</p>
  
  <p>Note that the ONLY change I made to the original code was to use a bigger .7z file.  Also note that it does immediately free up the RAM as soon as the extract is finished, but that doesn't help.  I feel like it is not freeing up RAM as it extracts like it should, or it is putting the entire contents into RAM until the very end when it is done and only then moving it out of RAM. Also, if I try to extract the same exact file using a mac app, while running instruments, I do not see the same behavior (StuffIt Expander for example maxed out at around 60MB of RAM while extracting the file, Keka, the open source mac archiver maxed out at 19MB of RAM). </p>
  
  <p>I'm not much of a mac/xcode/objective-c developer (yet) so any help with this would be greatly appreciated.  I could resort to using zip or rar instead, but I get far superior compression with LZMA so if at all possible I want to stick with this solution but obviously  I need to get it to work without crashing.</p>
  
  <p>Thanks!</p>
  
  <p><img src="https://i.stack.imgur.com/RGmjV.png" alt="Screenshot of Instruments.app profiling the example app"></p>