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. Dismiss Notice

Heap size grows and never returns memory to OS.

Discussion in 'Editor & General Support' started by GravelFrog, Apr 11, 2014.

  1. GravelFrog

    GravelFrog

    Joined:
    Jan 13, 2014
    Posts:
    2
    It seems like Unity never returns any OS memory when doing a GC. I have attached some code to verify this. If you allocate a lot of memory you will see the heap size grow, but then doing a full gc, it never reduces, only the used portion decreases. This would seem to indicate that once Unity allocates memory from the OS, it never returns it. Is this true?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class CreateGiantHeapThenDestroy : MonoBehaviour {
    7.  
    8.     private List<byte[]> byteArrayList = new List<byte[]>();
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19.  
    20.     void OnGUI() {
    21.  
    22.         uint heapsize = Profiler.GetMonoHeapSize();
    23.         uint usedmonosize = Profiler.GetMonoUsedSize();
    24.         uint reserved = Profiler.GetTotalReservedMemory();
    25.         uint unusedreserved = Profiler.GetTotalUnusedReservedMemory();
    26.  
    27.         GUILayout.Label(string.Format("Heapsize: {0}",heapsize/(1024*1024)));
    28.         GUILayout.Label(string.Format("Usedsize: {0}",usedmonosize/(1024*1024)));
    29.         GUILayout.Label (string.Format ("Reserved: {0}",reserved/(1024*1024)));
    30.         GUILayout.Label (string.Format ("UnusedReserved: {0}",unusedreserved/(1024*1024)));
    31.  
    32.         if(GUILayout.Button ("Run GC")) {
    33.             System.GC.Collect(1000,System.GCCollectionMode.Forced);
    34.         }
    35.  
    36.         if(GUILayout.Button("Allocate 100MB Memory")) {
    37.             byteArrayList.Add (new byte[1024*1024*100]);
    38.         }
    39.  
    40.         if(byteArrayList.Count>0) {
    41.             if(GUILayout.Button("Free 100MB Memory")) {
    42.                 byteArrayList.RemoveAt(byteArrayList.Count-1);
    43.             }
    44.  
    45.             if(GUILayout.Button ("Free All Memory")) {
    46.                 byteArrayList.Clear();
    47.             }
    48.         }
    49.     }
    50. }
    51.  
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Yes. Returning memory and asking for it again later is expensive. If your game needs X MB of data at time t, then the chances are it'll need X MB of data at time t+100.
     
  3. blastproofgames

    blastproofgames

    Joined:
    Apr 3, 2014
    Posts:
    18
    That might sound okay, but when you have an app with 32mb of texture memory and it goes up to 200mb, then gets killed by the os, that does NOT sound okay.

    I have an app with 3 scenes. It's a 2D Game.
    My texture memory never goes further than 32MB. It's usually around 25MB.
    In my second scene, the one with the highest cost, I have around 130MB.
    If I put the app in the background, open other apps that take lots of memory, it goes down to about 30MB.
    Then I can just reopen the app and just keep playing as usual. The memory goes up back to around 130MB in 5-10minutes whenever I change scenes.

    Why does unity reserve so much memory without returning it to the OS?
    I mean, if you have an app that can function with 30MB, Why request more memory when you already have 50MB?
    On older devices the app gets killed because the app requests too much memory even though it does NOT need it.

    Any chance this might be fixed?
     
  4. Pulas

    Pulas

    Joined:
    Feb 26, 2014
    Posts:
    10
    I encounter the same problem. How to solve it? Can anyone help me?