2

I have an iOS application which displays a map using the Google Maps Javascript API in a UIWebView. It seems that in iOS 7.0, something has changed in the UIWebView implementation that is causing map objects to not be released when there are no longer any references.

Using the XCode memory profiler, I see a 7-20 MB increase in RAM each time a map is created. Removing the map, and clearing references to it does not decrease the memory usage. Memory warnings do not seem to trigger Javascript garbage collection. When running the code on a physical device, the application logs memory warnings and inevitably will crash.

Here is some simplified Javascript which demonstrates the problem:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
            <title></title>

            <style type="text/css" media="screen">
                body { padding-top: 20px;}
                #container { width:100%; height:200px; }
                #container > div { height: 100%; width: 100%; }
            </style>

            <script type="text/javascript"
                src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>


            <script type="text/javascript" charset="utf-8">

                var domEl;

                function createMap() {
                    var container = document.getElementById( 'container' );


                    // Create fresh dom element
                    //
                    if( domEl ) {
                        container.removeChild( domEl );
                        console.log('removed old dom element');
                    }

                    domEl = document.createElement( 'div' );
                    container.appendChild( domEl );

                    // Create map
                    //
                    var mapOptions = {
                        center: new google.maps.LatLng( -34.397, 150.644 ),
                        zoom: 8,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    new google.maps.Map( domEl, mapOptions );

                    return false;
                }
            </script>

            </head>

    <body>

        <a href="#" onclick="createMap();">Create map</a>

        <div id="container"></div>

    </body>
</html>

I've also posted a complete XCode project on GitHub.

Has anyone seen this before? Can someone suggest a fix or workaround? In this case I have to use the Google Maps API; a native solution isn't an option.

sho
  • 759
  • 5
  • 16

0 Answers0