No, there is no BigMemory system for .Net (i.e., a heap memory manager without a GC), however you can minimize yours.
You can use an unmanaged heap to have unnecessary garbage collection, but if you are working with objects and not raw memory, you will have to serialize and deserialize them slow.
, , , , , , :
. .
. GC , .
, .
, . .
, \value, .
Protobuf, , .Net. + Get 425k \value. \.
, .
...
...
using ProtoBuf;
[TestFixture]
public class UnmanagedHeap
{
[Test]
public void UnmanagedHeapAccess()
{
const int Iterations = 425 * 1000;
const string Key = "woo";
Bling obj = new Bling { Id = -666 };
Cache cache = new Cache();
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < Iterations; i++)
{
cache.Put(Key, obj);
obj = cache.Get<Bling>(Key);
}
cache.Remove(Key);
Console.WriteLine(sw.Elapsed.TotalMilliseconds);
}
[DataContract]
public class Bling
{
[DataMember(Order = 1)]
public int Id { get; set; }
}
public class Cache
{
private const int SizeFieldWidth = 4;
private readonly Dictionary<string, IntPtr> _lookup = new Dictionary<string, IntPtr>();
public void Put(string key, object obj)
{
IntPtr oldPtr = _lookup.TryGetValue(key, out oldPtr) ? oldPtr : IntPtr.Zero;
IntPtr newPtr = SerializeToHeap(obj, oldPtr);
_lookup[key] = newPtr;
}
public T Get<T>(string key)
{
IntPtr ptr = _lookup[key];
return DeserializeFromHeap<T>(ptr);
}
public void Remove(string key)
{
IntPtr ptr = _lookup[key];
Marshal.FreeHGlobal(ptr);
_lookup.Remove(key);
}
private static IntPtr SerializeToHeap(object obj, IntPtr oldPtr)
{
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, obj);
byte[] objBytes = ms.GetBuffer();
int newSize = (int)ms.Length;
bool requiresAlloc = true;
if (oldPtr != IntPtr.Zero)
{
int oldSize = GetObjectSize(oldPtr);
requiresAlloc = (oldSize != newSize);
}
IntPtr newPtr = requiresAlloc ? Marshal.AllocHGlobal(newSize + SizeFieldWidth) : oldPtr;
byte[] sizeField = BitConverter.GetBytes(newSize);
Marshal.Copy(sizeField, 0, newPtr, SizeFieldWidth);
Marshal.Copy(objBytes, 0, newPtr + SizeFieldWidth, newSize);
return newPtr;
}
}
private static T DeserializeFromHeap<T>(IntPtr ptr)
{
int size = GetObjectSize(ptr);
byte[] objBytes = new byte[size];
Marshal.Copy(ptr + SizeFieldWidth, objBytes, 0, size);
using (MemoryStream ms = new MemoryStream(objBytes))
{
return Serializer.Deserialize<T>(ms);
}
}
private static int GetObjectSize(IntPtr ptr)
{
byte[] sizeField = new byte[SizeFieldWidth];
Marshal.Copy(ptr, sizeField, 0, SizeFieldWidth);
int size = BitConverter.ToInt32(sizeField, 0);
return size;
}
}
}