是否有类似.net的BigMemory系统

Ian*_*ose 5 .net garbage-collection terracotta scalability ehcache-bigmemory

我刚刚读到 BigMemory如何允许Java系统扩展而不是扩展.

关于BigMemory:

BigMemory使Java应用程序能够即时,轻松地访问大量内存,而不受垃圾收集的限制.

BigMemory是纯Java,提供进程内堆外缓存,允许您存储大量数据 - 最接近应用程序的TB级.

这一突破性解决方案通过独立和分布式缓存提高了内存利用率和应用程序性能.

那么我如何对.net做同样的事情,例如进程内,堆外缓存.(注意Asp.net缓存是在垃圾收集堆上)

Tim*_*oyd 8

不,没有.Net的BigMemory系统(即进程内非GC堆内存管理器),但是,您可以自己动手.

您可以利用非托管堆来获取非垃圾收集进程内堆,但是,如果您正在使用对象而不是原始内存,则必须对它们进行序列化和反序列化,这很慢.

您需要保持对堆信息的查找,以便您可以检索对象,这显然有自己的内存开销,因此不适合大量非常小的对象:

一个.管理对象将占用大量内存.
湾 GC将疯狂扫描管理对象.

如果对象足够大并且没有太多,这可能对你有用.

但是,您也可以将一些管理信息推送到非托管堆中.有很多优化机会.

这可以被包装起来像key\value cache一样工作,从而抽象堆信息和堆.

更新

更新了示例代码以使用Protobuf,它比.Net快得多地进行二进制序列化.这个简单的示例可以使用key\value包装器每秒放置+获取425k个对象.您的millage将根据对象大小\复杂程度而有所不同.

对象大小存储在非托管堆中,以减少托管堆上的内存消耗.

...
...
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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)