清除MKMapView的瓷砖缓存?
我正在开发一款使用MKMapView作为游戏场的iPhone游戏。在玩了几分钟之后,应用程序不可避免地开始变得缓慢,最终因内存不足而崩溃。挖掘罪魁祸首似乎是地图视图不断需要更多的记忆。游戏需要对地图进行大量的缩放和平移,因此我只能假设地图的平铺缓存一直在增长,直到内存耗尽为止。有没有办法强制地图视图刷新它的磁贴缓存或包含它的内存消耗?
I'm working on an iPhone game that uses an MKMapView as the playfield. After only a couple of minutes of play the app inevitably starts to get sluggish and eventually crashes due to low memory. After digging around the culprit seems to be that the map view constantly demands more memory. The game requires a lot of zooming and panning of the map so I can only assume that the map's cache of tiles just keeps growing until it runs out of memory. Is there any way to force the map view to flush it's cache of tiles or contain it's memory consumption?
*注意:此答案仅与iOS 4.1及更低版本相关。这个答案中描述的问题大多是在iOS 4.2中修复的。
我一直在做一些挖掘,因为我的应用程序同时使用地图和还有其他需要高RAM的功能。
I've been doing some digging on this as my app uses both the map and also has other features that demand high RAM.
我没有找到答案,但有一个解决方法。当您缩放到一个区域时,MKMapView的内存需求会呈指数级增长,并在该放大区域内平移。
I haven't found an answer, but a workaround. MKMapView's memory demands escalate exponentially as you zoom closer in to an area, and pan around within that zoomed in area.
MKMapView平铺缓存有两个级别。一个表现为乐器中的Malloc~196kb,另一个是不同大小的NSData(商店)。
There are two levels of MKMapView tile cache. One manifests itself as a Malloc ~196kb in Instruments, the other is NSData (store) of varying sizes.
Malloc似乎是活跃的使用中的瓷砖,并且有多少可以分配的上限。在我的应用程序中,该数字为16,不确定它是否基于UIView大小。这些分配似乎是严格管理的,它响应内存警告。
The Malloc appears to be the active in-use tiles, and there is a hard cap on how many can be allocated. In my app that number is 16, not sure if its based on UIView size or not. These allocations seem to be stringently managed, and it responds to memory warnings.
无论如何,在某个缩放级别,比如大陆级别(足以适应北美大部分地区)在iPad屏幕上),考虑到瓷砖的大小,如果从来没有真正必须达到第二级缓存(NSData(Store))以完成地图。一切都清爽干净。如果我将大量外部图像加载到活动内存中,则瓷砖会自行修剪。太棒了!
Anyway, at a certain zoom level, say, continent level (enough to fit most of North America in an iPad screen), given the size of the tiles, if never really has to get to that second level of caching (NSData (Store)) in order to complete the map. Everything is crisp and clean. If I load in a ton of external images into active memory, the tiles prune themselves. Awesome!
问题来自第二级缓存。当您放大时会发生这种情况,突然而不是16个瓷砖显示整个平面,它需要16个瓷砖才能展示Los Angelas,当您平移而不是仅仅倾倒那些旧瓷砖时,它会将它们放入NSData(商店) )它们似乎永远不会被释放的分配。
The problem comes when it hits that second level of caching. This happens when you zoom in, and suddenly instead of 16 tiles to show the entire planat, it needs 16 tiles just to show off Los Angelas, and as you pan around instead of just dumping those old tiles it puts them into the NSData (store) allocations where they seem to never get freed.
这个NSData(存储)是NSURLConnectionCache,它默认只存在于内存中。您无法访问此缓存来限制它,因为它不是默认的共享缓存(已经尝试过)。
This NSData (store) is the NSURLConnectionCache which exists by default only in memory. You can't access this cache to limit it, because it's not the default shared cache (already tried it).
所以这就是我遇到的问题。
So this is where I get stuck.
令人不满意的答案是,如果你禁用地图缩放并以相当宽的缩放级别修复它,你可以完全避免这个问题,但显然有些应用程序需要这个......那就是据我所知。
The unsatisfying answer is that if you disable map zooming and fix it at a reasonably broad zoom level, you can avoid this problem completely, but obviously some apps need this... and that's as far as I got.
我向Apple提交了一张支持票,看看他们是否可以透露任何方式限制地图的荒谬缓存(顺便提一下能够随便在活动内存中分配多达50+ mems的RAM。)
I filed a support ticket with Apple to see if they can divulge any way to limit this ridiculous cache for the map (which by the way I was able to casually crank up to 50+ megs of RAM allocated in active memory).
希望这会有所帮助。
编辑
下一个iOS版本似乎解决了这个无限缓存问题。 MKMapView现在积极修剪其缓存的磁贴数据。 REJOICE!
The next iOS release appears to have solved this limitless cache issue. MKMapView now aggressively prunes its cached tile data. REJOICE!