Search code examples
androidunity-game-engineunitywebrequest

GL_OUT_OF_MEMORY and android app crashing using unity?


I was trying to load images at run time into the Image Library from many image links for my AR project.While testing the below code my android app crashed with following errors

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.IO;

public class DynamicImageLib : MonoBehaviour
{
public ARTrackedImageManager aRTrackedImageManager;
public GameObject mTrackedImagePrefab;
public Texture2D imgToTexture2d;
public MutableRuntimeReferenceImageLibrary myRuntimeImageLibrary;
public Text msgText;
public Text numText;
bool isRunning = false;
List<string> allURLs = new List<string> { "https://i.picsum.photos/id/0/5616/3744.jpg", "https://i.picsum.photos/id/1001/5616/3744.jpg",
"https://i.picsum.photos/id/1000/5626/3635.jpg","https://i.picsum.photos/id/1004/5616/3744.jpg","https://i.picsum.photos/id/1005/5760/3840.jpg",
"https://i.picsum.photos/id/1006/3000/2000.jpg","https://i.picsum.photos/id/1008/5616/3744.jpg","https://i.picsum.photos/id/1009/5000/7502.jpg",
"https://i.picsum.photos/id/101/2621/1747.jpg","https://i.picsum.photos/id/1010/5184/3456.jpg","https://i.picsum.photos/id/1011/5472/3648.jpg",
"https://i.picsum.photos/id/1012/3973/2639.jpg","https://i.picsum.photos/id/1013/4256/2832.jpg","https://i.picsum.photos/id/1014/6016/4000.jpg",
"https://i.picsum.photos/id/1015/6000/4000.jpg","https://i.picsum.photos/id/1016/3844/2563.jpg","https://i.picsum.photos/id/1018/3914/2935.jpg",
"https://i.picsum.photos/id/1019/5472/3648.jpg","https://i.picsum.photos/id/102/4320/3240.jpg","https://i.picsum.photos/id/1020/4288/2848.jpg",
"https://i.picsum.photos/id/1021/2048/1206.jpg","https://i.picsum.photos/id/1022/6000/3376.jpg","https://i.picsum.photos/id/1023/3955/2094.jpg",
"https://i.picsum.photos/id/1024/1920/1280.jpg","https://i.picsum.photos/id/1025/4951/3301.jpg"};

void Awake()
{
    Screen.sleepTimeout = SleepTimeout.NeverSleep;
    aRTrackedImageManager = gameObject.AddComponent<ARTrackedImageManager>();
    aRTrackedImageManager.enabled = false;
    myRuntimeImageLibrary = aRTrackedImageManager.CreateRuntimeLibrary() as MutableRuntimeReferenceImageLibrary;
    aRTrackedImageManager.maxNumberOfMovingImages = 1;
    aRTrackedImageManager.trackedImagePrefab = mTrackedImagePrefab;
    aRTrackedImageManager.trackedImagesChanged += OnImageChanged;
}
// Start is called before the first frame update
void Start()
{
    Debug.Log("Intially Running................................................");
    // StartCoroutine(AddImageTrackerByUrl("https://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg"));

    //StartCoroutine(AddImageTrackerByUrl("https://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg[/URL]"));
    //StartCoroutine(AddImageTrackerByUrl("https://i.picsum.photos/id/0/5616/3744.jpg"));
    //StartCoroutine(AddImageTrackerByUrl("https://i.picsum.photos/id/1001/5616/3744.jpg"));


    StartCoroutine(AddImageTrackerByUrl());


}

// Update is called once per frame
void Update()
{

}

public void OnImageChanged(ARTrackedImagesChangedEventArgs args)
{
    foreach (var trackedImage in args.added)
    {
        Debug.Log(trackedImage.name);
        msgText.text = trackedImage.name;
    }
}

IEnumerator AddImageTrackerByUrl()
{

        isRunning = true;
        aRTrackedImageManager.enabled = false;
        if (aRTrackedImageManager.descriptor.supportsMutableLibrary)
        {
           foreach (var link in allURLs)
           {




                using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(link))
                {


                    yield return webRequest.SendWebRequest();

                    if (webRequest.isNetworkError || webRequest.isHttpError)
                    {
                        Debug.Log("Error is " + webRequest.error);
                    }
                    else
                    {

                        imgToTexture2d = DownloadHandlerTexture.GetContent(webRequest);

                        Unity.Jobs.JobHandle jobHandle = myRuntimeImageLibrary.ScheduleAddImageJob(imgToTexture2d, Path.GetFileName(link), 0.2f);
                        jobHandle.Complete();

                        if (myRuntimeImageLibrary != null)
                        {
                            Debug.Log("Image Library Count: " + myRuntimeImageLibrary.count);
                            numText.text = myRuntimeImageLibrary.count.ToString();
                            aRTrackedImageManager.referenceLibrary = myRuntimeImageLibrary;


                        }
                        webRequest.downloadHandler.Dispose();
                        imgToTexture2d = null;
                        aRTrackedImageManager.enabled = true;
                    }


                }
        }


    }



}

 }

The error from log is as follows

Error Unity OPENGL NATIVE PLUG-IN ERROR: GL_OUT_OF_MEMORY: Not enough memory left to execute command
Error Unity (Filename: ./Runtime/GfxDevice/opengles/GfxDeviceGLES.cpp Line: 353)
Error Unity OPENGL NATIVE PLUG-IN ERROR: GL_OUT_OF_MEMORY: Not enough memory left to execute command
Error Unity (Filename: ./Runtime/GfxDevice/opengles/GfxDeviceGLES.cpp Line: 353)
Error Unity Could not allocate memory: System out of memory!
Error Unity Trying to allocate: 72192000B with 16 alignment. MemoryLabel: NativeArray
Error Unity Allocation happened at: Line:59 in
Error Unity Memory overview
Error Unity [ ALLOC_TEMP_THREAD ] used: 32768B | peak: 0B | reserved: 2752512B
Error Unity [ ALLOC_TEMP_JOB_1_FRAME ] used: 0B | peak: 0B | reserved: 3145728B
Error Unity [ ALLOC_TEMP_JOB_2_FRAMES ] used: 0B | peak: 0B | reserved: 1048576B
Error Unity [ ALLOC_TEMP_JOB_4_FRAMES ] used: 0B | peak: 0B | reserved: 2097152B
Error Unity [ ALLOC_TEMP_JOB_ASYNC ] used: 4326745B | peak: 0B | reserved: 7340032B
Error Unity [ ALLOC_DEFAULT ] used: 15634711B | peak: 165212737B | reserved: 15974431B
Error Unity [ ALLOC_GAMEOBJECT ] used: 820820B | peak: 820952B | reserved: 823005B
Error Unity [ ALLOC_GFX ] used: 777844463B | peak: 777844463B | reserved: 777845964B
Error Unity (Filename: Line: 1449)

It initially downloads a few images and add to library and crashes.Is it because the downloaded images are high in size? I tried to free up size by using webrequest datahandler dispose method but no effect.How to solve memory related issues.Any guidelines?


Solution

  • Error Unity Could not allocate memory: System out of memory! 
    Error Unity Trying to allocate: 72192000B with 16 alignment. MemoryLabel: NativeArray
    

    It seems that the resolutions of your textures is very high. Also, they seem to be uncompressed.
    Texture size: 6000 * 4000 * 4(RGBA) = 96MB

    Use smaller or compressed textures. It is best to reduce the resolution of your images and compress them before uploading them to the GPU. Ideally texture sizes should be powers of two on the sides. These sizes are as follows: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 or 4096 pixels.