zoukankan      html  css  js  c++  java
  • (WeakReference )弱引用解决OutOfMemoryException异常 武胜

    使用WeakReference 来释放那些长时间在内存中无用的大对象

    测试代码:

    View Code
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Binary;
    namespace WeakReferenceTestSample
    {
        public class SomeBigClass : List<string>
        {
    
            public SomeBigClass()
            {
                //this.LoadBigObject();
                this.LoadWeakReferenceBigObject();
            }
    
            private void LoadBigObject()
            {
                var smallBlockSize = 90000;
                var largeBlockSize = 1 << 24;
                var count = 0;
                var bigBlock = new byte[0];
                //for (int i = 0; i < 100000; i++)
                //    this.Add(string.Format("String No. {0}", i));
                try
                {
                    var smallBlocks = new List<byte[]>();
                    while (true)
                    {
                        GC.Collect();
                        bigBlock = new byte[largeBlockSize];
                        largeBlockSize++;
                        smallBlocks.Add(new byte[smallBlockSize]);
                        count++;
                        Console.WriteLine("{0} {1}", count.ToString(), ObjectSize(smallBlocks).ToString());
                    }
                }
                catch (OutOfMemoryException)
                {
                    bigBlock = null;
                    GC.Collect();
                    Console.WriteLine("{0} Mb allocated",
                                      (count*smallBlockSize)/(1024*1024));
                }
            }
    
            private void LoadWeakReferenceBigObject()
            {
                var smallBlockSize = 90000;
                var largeBlockSize = 1 << 24;
                var count = 0;
                //var bigBlock = new byte[0];
                var bigBlock = new WeakReference(new byte[0]);
                try
                {
                    //var smallBlocks = new List<byte[]>();
                    var smallBlocks = new List<WeakReference>();
                    while (true)
                    {
                        GC.Collect();
                        bigBlock = new WeakReference(new byte[largeBlockSize]);
                        largeBlockSize++;
                        smallBlocks.Add(new WeakReference(new byte[smallBlockSize]));
                        count++;
                        Console.WriteLine("{0} {1}", count.ToString(), ObjectSize(smallBlocks).ToString());
                    }
                }
                catch (OutOfMemoryException)
                {
                    bigBlock = null;
                    GC.Collect();
                    Console.WriteLine("{0} Mb allocated",
                        (count * smallBlockSize) / (1024 * 1024));
                }
            }
    
            private long ObjectSize(object o)
            {
                long size = 0;
                //object o = new object();
                using (Stream s = new MemoryStream())
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(s, o);
                    size = s.Length;
                }
                return size;
            }
        }
    }
  • 相关阅读:
    Android Context
    Java 字节数组 对比 低速 指针快速
    private、protected、public、published 访问限制(或者叫类成员的可见性)
    读“变革中的思索”
    微软全球资深副总裁张亚勤先生力作——《变革中思索》连载
    这个冬天,我以《监控》下酒
    震撼 中国的史蒂芬金——读小说《监控》有感
    《监控》新派惊悚职场小说
    鱼与飞鸟的距离
    博文视点大讲堂第21期免费讲座:解密Google、百度——搜索引擎揭秘
  • 原文地址:https://www.cnblogs.com/zeroone/p/3025667.html
Copyright © 2011-2022 走看看