Search Unity

Bug AR Foundation add reference images at runtime causes memory leak on iOS, but works fine on Android.

Discussion in 'AR' started by SoumyadeepSen, Apr 22, 2022.

  1. SoumyadeepSen

    SoumyadeepSen

    Joined:
    Jul 21, 2018
    Posts:
    1
    I want to add 500 reference images at runtime, for now I'm adding up to 250. This goes well on android, but not for iOS. Memory usage goes up to 1.6 GB until the app crashes.

    Standard memory usage for my app is around 300 MB with lots of dynamic image thumbnails (approx. count 300) and other stuff.

    After adding 250 reference images, runtime memory usage goes up to around 0.55GB on Android, but for the same scenario it goes up to 1.6GB on iOS and then the app crashes.

    Unity version: 2021.1.19f1
    AR Foundation: 4.2.2
    Android: Motorola One Fusion+ (Android 11, 6GB)
    iPhone: iPhone XR (iOS 15.0.2, 3GB)

    I'm adding the code snippet below, any help will be appreciated, thanks in advance.

    Code (CSharp):
    1.  
    2. private void Start()
    3. {
    4.     //Other codes...
    5.     StartCoroutine(AddToLibrary(mutableLibrary, true));
    6. }
    7. private IEnumerator AddToLibrary(MutableRuntimeReferenceImageLibrary mutableLibrary, bool wait)
    8. {
    9.     foreach (var item in markers)
    10.     {
    11.         using (UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(item.Value))
    12.         {
    13.             yield return unityWebRequest.SendWebRequest();
    14.             if (unityWebRequest.result != UnityWebRequest.Result.Success)
    15.             {
    16.                 Debug.LogError(unityWebRequest.error);
    17.             }
    18.             else
    19.             {
    20.                 Texture2D texture2D = DownloadHandlerTexture.GetContent(unityWebRequest);
    21.                 if (texture2D && imageManager && mutableLibrary != null)
    22.                 {
    23.                     var jobState = mutableLibrary.ScheduleAddImageWithValidationJob(texture2D, item.Key, 1);
    24.                     yield return new WaitWhile(() => wait && !jobState.jobHandle.IsCompleted);  //Optional wait
    25.                     Destroy(texture2D);
    26.                 }
    27.             }
    28.         }
    29.         yield return new WaitForEndOfFrame();
    30.     }
    31. }
     
    Last edited: Apr 22, 2022
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    I recommend trying unity 2021.3 LTS (no big reason to stay on 2021 tech stream rn), update all AR tools like AR foundation, arkit, etc om the package manager if possible, then test again.

    If the probably still occurs file a bug report. Since you're on the newest LTS they should be handles fairly quickly
     
  3. sahilushaikh6

    sahilushaikh6

    Joined:
    Dec 4, 2018
    Posts:
    3
  4. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Last edited: Nov 15, 2022
  6. Liminal-Ridges

    Liminal-Ridges

    Joined:
    Oct 21, 2015
    Posts:
    256
    anyone managed to fix it?
     
  7. WyattBUnity

    WyattBUnity

    Unity Technologies

    Joined:
    Mar 27, 2023
    Posts:
    46
    As far as the bug where ScheduleAddImageWithValidationJob not actually adding an image to the library, we are going to be updating the docs to mention that you will need to assure that
    Code (CSharp):
    1. ARSession.state == ARSessionState.Ready || ARSession.state == ARSessionState.SessionTracking
    because if it not in one of the states of being ready, then it will finish the job with an error in one of the returned pieces, and the library will not have anything added to it.
     
  8. Liminal-Ridges

    Liminal-Ridges

    Joined:
    Oct 21, 2015
    Posts:
    256
    @WyattBUnity It says session tracking but my problem is that once i run ScheduleAddImageWithValidationJob it crashes. I dont even know if it adds the image (i wanna create runtime targets so i take a camera frame as a target)

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         library = m_TrackedImageManager.CreateRuntimeLibrary();
    4.         m_TrackedImageManager.trackedImagesChanged += OnChanged;
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         if (Input.GetMouseButtonUp(0))
    10.             if (library is MutableRuntimeReferenceImageLibrary mutableLibrary)
    11.                 mutableLibrary.ScheduleAddImageWithValidationJob((Texture2D)Camera.main.GetComponent<ARCameraBackground>().material.GetTexture("_MainTex"), "my new image", 0.1f);
    12.     }
     
    Last edited: Apr 14, 2023
  9. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062
    Liminal-Ridges likes this.