Get the Amount of Available Memory for Allocation
Updated: Dec-26,2020
using System; public class MeasureSize<T> { private const int NumberOfInstances = 100000; private T[][] _memArray; public MeasureSize() { _memArray = new T[NumberOfInstances][]; } public long GetByteSize() { var oldSize = GC.GetTotalMemory(true); for (var i = 0; i < NumberOfInstances; i++) _memArray[i] = new T[1]; var newSize = GC.GetTotalMemory(true); GC.KeepAlive(_memArray); if (oldSize > newSize) return GetByteSize(); return (newSize - oldSize) / NumberOfInstances; } public long GetByteSize(int size) { _memArray = new T[size][]; var oldSize = GC.GetTotalMemory(true); for (var i = 0; i < size; i++) _memArray[i] = new T[1]; var newSize = GC.GetTotalMemory(true); GC.KeepAlive(_memArray); if (oldSize > newSize) return GetByteSize(size); return (newSize - oldSize) / size; } }