Heap size grows and never returns memory to OS.

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?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CreateGiantHeapThenDestroy : MonoBehaviour {

	private List<byte[]> byteArrayList = new List<byte[]>();

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI() {

		uint heapsize = Profiler.GetMonoHeapSize();
		uint usedmonosize = Profiler.GetMonoUsedSize();
		uint reserved = Profiler.GetTotalReservedMemory();
		uint unusedreserved = Profiler.GetTotalUnusedReservedMemory();

		GUILayout.Label(string.Format("Heapsize: {0}",heapsize/(1024*1024)));
		GUILayout.Label(string.Format("Usedsize: {0}",usedmonosize/(1024*1024)));
		GUILayout.Label (string.Format ("Reserved: {0}",reserved/(1024*1024)));
		GUILayout.Label (string.Format ("UnusedReserved: {0}",unusedreserved/(1024*1024)));

		if(GUILayout.Button ("Run GC")) {
			System.GC.Collect(1000,System.GCCollectionMode.Forced);
		}

		if(GUILayout.Button("Allocate 100MB Memory")) {
			byteArrayList.Add (new byte[1024*1024*100]);
		} 

		if(byteArrayList.Count>0) {
			if(GUILayout.Button("Free 100MB Memory")) {
				byteArrayList.RemoveAt(byteArrayList.Count-1);
			}

			if(GUILayout.Button ("Free All Memory")) {
				byteArrayList.Clear();
			}
		}
	}
}

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.

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?

I encounter the same problem. How to solve it? Can anyone help me?