RPlay
RPlay

Reputation: 361

GMSMapView clear doesn't clear memory

I have a GMSMapView in my app. The app with an open map screen takes 150 mb in memory.

At some point I add a lot of polygons. After that the application takes 230 mb.

After calling the method [GMSMapView clear] all polygons disappear, but app still takes 230 mb in memory.

Pointers to Polygons are not stored anywhere else. How to make the map clear the memory an why it does not happen after calling 'clear' method?

Upvotes: 2

Views: 1871

Answers (2)

Zanael
Zanael

Reputation: 760

Well, this is pain in a**... I not found the official docs.

My solution:

Swift 3.0

1) Do not use Storyboard, create mapView in code:

@IBOutlet weak fileprivate var mapViewContainer: UIView!

weak fileprivate var mapView: GMSMapView? {
    didSet { 
        //you can config and customise your map here
        self.configureMapView() 
    }
}

//If you use clustering
fileprivate var clusterManager: ClusterManager?
fileprivate var clusterRenderer: ClusterRenderer?

2) Create viewController configure function & call it before viewDidLoad:

func configure() {
    mapView = GMSMapView()
    mapView?.delegate = self
}

3) Layout & add mapView to your UI:

override func viewDidLoad() {
    super.viewDidLoad()

    if let mapView = mapView {
        mapViewContainer.addSubview(mapView)
        //Create constraints as you wish
        mapView.fillView()
        mapViewContainer.layoutSubviews()
    }
}

4) Now create function:

func releaseMemory() {

    if
        let mapView = mapView,
        let clusterManager = clusterManager {

        clusterManager.clearItems()
        mapView.clear()
        mapView.delegate = nil
        mapView.removeFromSuperview()

    }

    clusterManager = nil
    clusterRenderer = nil
    mapView = nil
}

You call it in ViewDidDisappear, or call delegate...

releaseMemory() function clear memory, not so smart, but it works.

Upvotes: 5

Efren
Efren

Reputation: 4907

You might try storing the polygon objects somewhere and then call polygon.map = nil on all of them, delete the polygon references and then call the map view clear

Upvotes: 2

Related Questions