当前位置: 首页>>代码示例>>C#>>正文


C# MemoryCache.Dispose方法代码示例

本文整理汇总了C#中System.Runtime.Caching.MemoryCache.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryCache.Dispose方法的具体用法?C# MemoryCache.Dispose怎么用?C# MemoryCache.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.Caching.MemoryCache的用法示例。


在下文中一共展示了MemoryCache.Dispose方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MemorySizePerformance

        private static void MemorySizePerformance()
        {
            /*
             * Some numbers:
             * ---------------------------------------------------------------------------------------------------------
             * 10k objects
             * ---------------------------------------------------------------------------------------------------------
             * maxStringSize            repeatStringSize            serialized vs Raw            compression vs raw
             * 1                        1                            92.4%                        93.8%
             * 17                       1                            88.7%                        90.0%
             * 128                      1                            73.9%                        74.7%
             * 2                        2                            92.7%                        93.8%
             * 17                       2                            86.7%                        89.7%
             * 128                      2                            73.9%                        74.7%
             * 4                        4                            90.5%                        91.6%
             * 17                       4                            86.7%                        89.7%
             * 128                      4                            73.9%                        68.8%
             * 128                      128                          73.9%                        56.8%
             * 
             * 10,000                   10,000                       50.7%                         1.8%
             * 100,000                  100,000                      50.1%                         0.2%
             */

            const int MAX_STRING_SIZE = 10000;
            const int REPEAT_STRING_SIZE = 10;
            const int ITERATIONS = 1000;
            const string KEY = "impt-key";

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            long mCs = GC.GetTotalMemory(true);
            MemoryCache cM = new MemoryCache("cM");
            for (int i = 0; i < ITERATIONS; i++)
            {
                var m = new MultipleProperties
                    {
                        Id = GenerateId(),
                        Name = GenerateString(MAX_STRING_SIZE, REPEAT_STRING_SIZE)
                    };

                byte[] s = ProtoBufSerializer.Serialize(m);
                byte[] c = SmartCompressor.Instance.CompressAsync(s).Result;
                cM.Set(KEY + i.ToString(CultureInfo.InvariantCulture), c, null);
            }

            long mCe = GC.GetTotalMemory(true);
            long compressMemory = mCe - mCs;

            cM.Trim(100);
            cM.Dispose();
            // ReSharper disable once RedundantAssignment
            cM = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            long mSs = GC.GetTotalMemory(true);
            MemoryCache sM = new MemoryCache("sM");
            for (int i = 0; i < ITERATIONS; i++)
            {
                var m = new MultipleProperties
                    {
                        Id = GenerateId(),
                        Name = GenerateString(MAX_STRING_SIZE, REPEAT_STRING_SIZE)
                    };

                byte[] s = ProtoBufSerializer.Serialize(m);
                sM.Set(KEY + i.ToString(CultureInfo.InvariantCulture), s, null);
            }

            long mSe = GC.GetTotalMemory(true);
            long serializeMemory = mSe - mSs;

            sM.Trim(100);
            sM.Dispose();
            // ReSharper disable once RedundantAssignment
            sM = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            long mRs = GC.GetTotalMemory(true);
            MemoryCache rM = new MemoryCache("rM");
            for (int i = 0; i < ITERATIONS; i++)
            {
                var m = new MultipleProperties
                    {
                        Id = GenerateId(),
                        Name = GenerateString(MAX_STRING_SIZE, REPEAT_STRING_SIZE)
                    };
                rM.Set(KEY + i.ToString(CultureInfo.InvariantCulture), m, null);
            }

//.........这里部分代码省略.........
开发者ID:JesseBuesking,项目名称:BB.Caching,代码行数:101,代码来源:SerializationAndCompressionTests.cs


注:本文中的System.Runtime.Caching.MemoryCache.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。