3

I'm fetching about 136 images, each one about 500 KB, in order to have them cached on the disk.

After downloading image #98, I start getting the following error for the images left (which makes me think they aren't getting cached).

2015-07-29 09:52:44.471 MyProject[299:3418965] [HANEKE][ERROR] Failed to get data for key https://s3.amazonaws.com/my_bucket/my_image_n_99.jpg
Jul 29 09:52:45 my.host.net MyProject[299] <Error>: CGBitmapContextInfoCreate: unable to allocate 31492608 bytes for bitmap data
MyProject(299,0xb039f000) malloc: *** mach_vm_map(size=31494144) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

My first guess was the memory cache filled up, so I called HanekeSwift's Cache.onMemoryWarning() (had to make it public) since it has the following implementation:

for (_, (_, memoryCache, _)) in self.formats {
    memoryCache.removeAllObjects()
}

But even tho I called it (and supposedly it should clear the memory cache), I still get the error, so I don't know what's wrong.

Any ideas?

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

2 Answers2

0

I had the same problem, and I did as you did, make Cache.onMemoryWarning() public and then call Shared.imageCache.onMemoryWarning() in the method didRecieveMemoryWarning().

And it worked!

RaptoX
  • 2,565
  • 2
  • 14
  • 15
0

For me previous answers did improve but not fully solve my out of memory problems. I could even get the app to crash without there being time to print a memory warning notice to the console.

What I had was two Haneke caches in my app. UIImage cache and Data cache. When both of them were fetching significant amounts of data simultaneously they did not seem able to keep memory in check.

I removed my UIImage cache and used only my generic Data cache. Anywhere the fetch was dealing with an image urls and wanted a UIImage I coerced the data to a UIImage let image = UIImage(data: data)! and the rest of my code worked as before.

This seems to be much more stable for me so far when dealing with loading hundreds of images and video files at the same time.

The fancy built-in image helpers require a UIImage cache. You can get to the same old behavior by extending UIImage yourself, copying the code for the helpers from Haneke, hooking into your Data cache and doing the coercion of needed.

Martin Westin
  • 1,419
  • 1
  • 14
  • 25
  • I do one more trick... I have a second disk-only cache implementation that downloads from the network but does not put anything in memory. That should be orthogonal to this issue but does improve the perceived performance quite a bit. – Martin Westin Oct 25 '19 at 13:33