Reputation: 68770
I'm looking for a way to process many images, but not run out of memory. When my app first runs it has to create a number of thumbnails and store them locally. This only happens one time.
I'm getting out of memory errors though.
Roughly I am going this:
[Loop BigPaths]
sourceImg = UIImage.FromFile (nextBigpath);
img = UIImageTools.MakeSquare (sourceImg, 50,3);
img.AsJPEG ().Save (path, NSDataWritingOptions.Atomic, out err);
sourceImg.Dispose ();
sourceImg = null;
img.Dispose ();
img = null;
[end loop]
UPDATE:
To fix it, I am now calling GC.Collect() after every 10 items, waiting 50 milliseconds in a Thread.Sleep().
I also made sourceImg
img
defined outside the loop.
With these 2 changes, I am no longer getting memory errors.
Upvotes: 2
Views: 645
Reputation: 68770
This is the solution I found worked.
Create the image variables outside the loop and reuse them, making sure that after each use .Dispose() is called.
After 50 images call GC.Collect() then Thread.Sleep(50) to give the GC time to clean up.
After that there were no memory issues.
Upvotes: 1