Memory leak in Unity

IEnumerator GetGoogleMap(float latitude, float longitude)
    {
        print ("latitude " + latitude);
        print ("longitude " + longitude);
        url = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude +
            "&zoom=" + zoom + "&size=" + mapWidth + "x" + mapHeight + "&maptype=" + mapSelected +
            "&markers=color:red%7Clabel:A%7C" + latitude + "," + longitude + "&key=AIzaSyDK04pO2JEC4C01AQSW9dpuBDunvtuA-o8";
        WWW www = new WWW(url);
        yield return www;
        myMap.GetComponent<RawImage>().texture = www.texture;
        StopCoroutine (mapCoroutine);

    }

I got a memory leak when I continuous calling this chunk of code. The leak happens with the line “myMap.GetComponent().texture = www.texture;” as I tested. I dunno y the program seems to store the image I request from the website all the time. I can’t find a way to free those images. Can anyone help me out?

myMap is just a RawImage and I called this chunk of code in update every single seconds.

The Unity doc describes a memory leak issue that can occur Unity - Scripting API: WWW.texture

1 Like

Thanks a lot Brathnann!

IEnumerator GetGoogleMap(float latitude, float longitude)
    {
        print ("latitude " + latitude);
        print ("longitude " + longitude);
        url = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude +
            "&zoom=" + zoom + "&size=" + mapWidth + "x" + mapHeight + "&maptype=" + mapSelected +
            "&markers=color:red%7Clabel:A%7C" + latitude + "," + longitude + "&key=AIzaSyDK04pO2JEC4C01AQSW9dpuBDunvtuA-o8";
        WWW www = new WWW(url);
        yield return www;
        Texture mapTexture = www.texture;
        myMap.GetComponent<RawImage>().texture = mapTexture;
        StopCoroutine (mapCoroutine);
        Destroy(mapTexture);
    }

I changed my code like this. The memory leak seems gone but I don’t have my image anymore. It is always a blank image(whole white). Do you know what could cause that?

Your destroying the MapTexture, thats why its coming out white.

Use www.LoadImageIntoTexture instead that should stop the leak and remove the need for the Destroy.

I just tried with the LoadImageIntoTexture, still giving me a blank picture. :frowning:

IEnumerator GetGoogleMap(float latitude, float longitude)
    {
        print ("latitude " + latitude);
        print ("longitude " + longitude);
        url = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude +
            "&zoom=" + zoom + "&size=" + mapWidth + "x" + mapHeight + "&maptype=" + mapSelected +
            "&markers=color:red%7Clabel:A%7C" + latitude + "," + longitude + "&key=AIzaSyDK04pO2JEC4C01AQSW9dpuBDunvtuA-o8";
        WWW www = new WWW(url);
        yield return www;
        www.LoadImageIntoTexture((Texture2D)myMap.GetComponent<RawImage>().texture);
        StopCoroutine (mapCoroutine);
    }

Here is my code

Maybe try assigning it to a variable Unity - Scripting API: WWW.LoadImageIntoTexture Like they are doing in their example and see if that matters.

In addition to the above notes, the WWW class also implements the IDisposable interface, which means you’re required to .Dispose() of it afterwards.

The best way is usually with a “using” block, like so:

 using (WWW www = new WWW(url)) {
    yield return www;
    // use the results here
 }

Unfortunately, 99% of the example code out there (including Unity’s sample code) does not properly follow the IDisposable contract. :confused:

Thanks guys I added a yield return new waitForSeconds and the problem seems to be solved