Fixed size arrays don't seem to get garbage collected properly, is there a known workaround?
In the following script the local variable data in the Start function creates a large array (the type doesn't matter, I've tried it with non-value types) which isn't used anywhere. Garbage collection is called explicitly (and repeatedly) in the Update function but the memory associated with the array does not appear to ever get freed. You can verify this by removing the array creation from the Start function and comparing the memory usage in the profiler window.
using UnityEngine;
using System.Collections;
public class MemoryLeak : MonoBehaviour
{
// Use this for initialization
void Start()
{
Color[] data = new Color[ 1024 * 1024 * 16 ];
}
// Update is called once per frame
void Update()
{
if( Time.frameCount % 128 == 0 )
{
Debug.Log( "Garbage collection" );
System.GC.Collect();
}
}
}