xcode 每次加载视图时 MKMapView 使用大量内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36417350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:41:48  来源:igfitidea点击:

MKMapView using a lot of memory each time i load its view

iosxcodeswiftmkmapview

提问by Richie

I have a pretty simple app, with a couple of view controllers. There is a MKMapView in the second view controller. It is set up correctly, and functions fine. The problem is, each time I load its view the Memory usage jumps ~30mb, and never goes back down, so each time i go into the view it keeps jumping and eventually gets super high. I tried removing the map view when i leave the controller like this:

我有一个非常简单的应用程序,有几个视图控制器。第二个视图控制器中有一个 MKMapView。它设置正确,功能正常。问题是,每次我加载它的视图时,内存使用量会跳跃~30mb,并且永远不会下降,所以每次我进入视图时它都会不断跳跃并最终变得超高。当我像这样离开控制器时,我尝试删除地图视图:

override func viewWillDisappear(animated: Bool) {
        map.removeFromSuperview()
    }

but it doesn't have any effect on the memory. The map views delegate is set to its view controller.

但它对内存没有任何影响。地图视图委托设置为其视图控制器。

I tried checking for leaks using Xcode instruments but didn't find anything.

我尝试使用 Xcode 工具检查泄漏,但没有找到任何东西。

Does anyone know how to fix this?

有谁知道如何解决这一问题?

Thanks

谢谢

EDIT: Adding this seems to work:

编辑:添加这个似乎有效:

func removeNastyMapMemory() {
        map.mapType = MKMapType.Hybrid
        map.delegate = nil
        map.removeFromSuperview()
        map = nil
    }

    override func viewWillDisappear(animated: Bool) {
        removeNastyMapMemory()
    }

采纳答案by Hemang

This is not Swift issue, is coming from Objective-C days. The possible ways to handle this issue is depending upon the situation and behavior of the app.

这不是 Swift 问题,而是来自 Objective-C 时代。处理此问题的可能方法取决于应用程序的情况和行为。

  1. If you're using a Map for multiple times (or places), only create a single (shared) instance of it. Which you can use it whenever you want.

  2. Or If you're only using it for once, then try a solution from here, https://stackoverflow.com/a/25419783/1603234. This may help. Reduce little. But not all.

  1. 如果您多次(或多次)使用 Map,只需创建它的单个(共享)实例。您可以随时使用它。

  2. 或者,如果您只使用一次,请从这里尝试解决方案,https://stackoverflow.com/a/25419783/1603234。这可能会有所帮助。减少一点。但不是所有的。