Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

GL_OUT_OF_MEMORY and android app crashing using AR Foundation?

Discussion in 'AR' started by zyonneo, Apr 21, 2020.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.XR.ARSubsystems;
    7. using UnityEngine.UI;
    8. using UnityEngine.Networking;
    9. using System.IO;
    10.  
    11. public class DynamicImageLib : MonoBehaviour
    12. {
    13. public ARTrackedImageManager aRTrackedImageManager;
    14. public GameObject mTrackedImagePrefab;
    15. public Texture2D imgToTexture2d;
    16. public MutableRuntimeReferenceImageLibrary myRuntimeImageLibrary;
    17. public Text msgText;
    18. public Text numText;
    19. bool isRunning = false;
    20. List<string> allURLs = new List<string> { "https://i.picsum.photos/id/0/5616/3744.jpg", "https://i.picsum.photos/id/1001/5616/3744.jpg",
    21. "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",
    22. "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",
    23. "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",
    24. "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",
    25. "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",
    26. "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",
    27. "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",
    28. "https://i.picsum.photos/id/1024/1920/1280.jpg","https://i.picsum.photos/id/1025/4951/3301.jpg"};
    29.  
    30. void Awake()
    31. {
    32.     Screen.sleepTimeout = SleepTimeout.NeverSleep;
    33.     aRTrackedImageManager = gameObject.AddComponent<ARTrackedImageManager>();
    34.     aRTrackedImageManager.enabled = false;
    35.     myRuntimeImageLibrary = aRTrackedImageManager.CreateRuntimeLibrary() as MutableRuntimeReferenceImageLibrary;
    36.     aRTrackedImageManager.maxNumberOfMovingImages = 1;
    37.     aRTrackedImageManager.trackedImagePrefab = mTrackedImagePrefab;
    38.     aRTrackedImageManager.trackedImagesChanged += OnImageChanged;
    39. }
    40. // Start is called before the first frame update
    41. void Start()
    42. {
    43.     Debug.Log("Intially Running................................................");
    44.     // StartCoroutine(AddImageTrackerByUrl("https://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg"));
    45.  
    46.     //StartCoroutine(AddImageTrackerByUrl("https://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg[/URL]"));
    47.     //StartCoroutine(AddImageTrackerByUrl("https://i.picsum.photos/id/0/5616/3744.jpg"));
    48.     //StartCoroutine(AddImageTrackerByUrl("https://i.picsum.photos/id/1001/5616/3744.jpg"));
    49.  
    50.  
    51.     StartCoroutine(AddImageTrackerByUrl());
    52.  
    53.  
    54. }
    55.  
    56. // Update is called once per frame
    57. void Update()
    58. {
    59.  
    60. }
    61.  
    62. public void OnImageChanged(ARTrackedImagesChangedEventArgs args)
    63. {
    64.     foreach (var trackedImage in args.added)
    65.     {
    66.         Debug.Log(trackedImage.name);
    67.         msgText.text = trackedImage.name;
    68.     }
    69. }
    70.  
    71. IEnumerator AddImageTrackerByUrl()
    72. {
    73.  
    74.         isRunning = true;
    75.         aRTrackedImageManager.enabled = false;
    76.         if (aRTrackedImageManager.descriptor.supportsMutableLibrary)
    77.         {
    78.            foreach (var link in allURLs)
    79.            {
    80.  
    81.  
    82.  
    83.  
    84.                 using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(link))
    85.                 {
    86.  
    87.  
    88.                     yield return webRequest.SendWebRequest();
    89.  
    90.                     if (webRequest.isNetworkError || webRequest.isHttpError)
    91.                     {
    92.                         Debug.Log("Error is " + webRequest.error);
    93.                     }
    94.                     else
    95.                     {
    96.  
    97.                         imgToTexture2d = DownloadHandlerTexture.GetContent(webRequest);
    98.  
    99.                         Unity.Jobs.JobHandle jobHandle = myRuntimeImageLibrary.ScheduleAddImageJob(imgToTexture2d, Path.GetFileName(link), 0.2f);
    100.                         jobHandle.Complete();
    101.  
    102.                         if (myRuntimeImageLibrary != null)
    103.                         {
    104.                             Debug.Log("Image Library Count: " + myRuntimeImageLibrary.count);
    105.                             numText.text = myRuntimeImageLibrary.count.ToString();
    106.                             aRTrackedImageManager.referenceLibrary = myRuntimeImageLibrary;
    107.  
    108.  
    109.                         }
    110.                         webRequest.downloadHandler.Dispose();
    111.                         imgToTexture2d = null;
    112.                         aRTrackedImageManager.enabled = true;
    113.                     }
    114.  
    115.  
    116.                 }
    117.         }
    118.  
    119.  
    120.     }
    121.  
    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.Later I used small sized images i added around 39 of them and it downloaded only 36 missed some pics.How to solve memory related issues.Any guidelines?
     
  2. darshanpv

    darshanpv

    Joined:
    Feb 1, 2019
    Posts:
    7
    I am also getting similar issue after I was able to implement runtime reference library implementation. It runs for sometime and then crashes on Motorola device. Moto Z2