是否有类似.net的BigMemory系统

时间:2011-08-25 10:54:17

标签: .net garbage-collection scalability terracotta ehcache-bigmemory

我刚刚reading关于BigMemory如何允许Java系统扩展而不是扩展。

关于BigMemory

  

BigMemory为Java应用程序提供即时,轻松的访问权限   大内存占用,没有垃圾收集的限制。

     

BigMemory是纯Java,提供进程内,堆外缓存   让您存储大量数据 - 最接近您的数据   应用。

     

这一突破性解决方案可提高内存利用率   独立和分布式的应用程序性能   缓存。

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

2 个答案:

答案 0 :(得分:8)

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

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

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

一个。管理对象将占用大量内存 湾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;
        }
    }
}

答案 1 :(得分:1)

是的,有100%的托管代码。 在上面的答案中建议的ProtoBuf不会给你100%的透明度  它没有正确映射多态ereferences和循环+需要特殊属性。 NFX Pile不需要[Serializable]

以外的任何内容

https://github.com/aumcode/nfx https://github.com/aumcode/nfx/blob/master/Source/NFX/ApplicationModel/Pile/IPile.cs

https://github.com/aumcode/nfx/blob/master/Source/NFX/ApplicationModel/Pile/ICache.cs

观看视频: https://www.youtube.com/watch?v=IUBF2Ncvbbs

https://www.youtube.com/watch?v=Dz_7hukyejQ

Apache 2.0