弱引用如何有效避免OutOfMemoryException异常发生?

2026-05-22 03:386阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计403个文字,预计阅读时间需要2分钟。

弱引用如何有效避免OutOfMemoryException异常发生?

使用WeakReference释放长时间内存中无用的对象测试代码:

csharpusing System;using System.Collections.Generic;using System.WeakReference;

public class ViewCode{ public static void Main() { // 创建一个包含大量对象的列表 List weakReferences=new List();

for (int i=0; i <1000; i++) { LargeObject obj=new LargeObject(); weakReferences.Add(new WeakReference(obj)); }

// 清除引用,让对象可以被垃圾回收 foreach (WeakReference weakRef in weakReferences) { weakRef.Target=null; }

// 检查对象是否被回收 int stillAliveCount=0; foreach (WeakReference weakRef in weakReferences) { if (weakRef.IsAlive) { stillAliveCount++; } }

Console.WriteLine($仍有 {stillAliveCount} 个对象未被回收。); }}

public class LargeObject{ // 大对象,包含大量数据}

使用WeakReference来释放那些长时间在内存中无用的大对象测试代码:ViewCodeusingSystem;usingSystem.Collections.Generic

使用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{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();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();var smallBlocks new List();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;}}}

 

弱引用如何有效避免OutOfMemoryException异常发生?

本文共计403个文字,预计阅读时间需要2分钟。

弱引用如何有效避免OutOfMemoryException异常发生?

使用WeakReference释放长时间内存中无用的对象测试代码:

csharpusing System;using System.Collections.Generic;using System.WeakReference;

public class ViewCode{ public static void Main() { // 创建一个包含大量对象的列表 List weakReferences=new List();

for (int i=0; i <1000; i++) { LargeObject obj=new LargeObject(); weakReferences.Add(new WeakReference(obj)); }

// 清除引用,让对象可以被垃圾回收 foreach (WeakReference weakRef in weakReferences) { weakRef.Target=null; }

// 检查对象是否被回收 int stillAliveCount=0; foreach (WeakReference weakRef in weakReferences) { if (weakRef.IsAlive) { stillAliveCount++; } }

Console.WriteLine($仍有 {stillAliveCount} 个对象未被回收。); }}

public class LargeObject{ // 大对象,包含大量数据}

使用WeakReference来释放那些长时间在内存中无用的大对象测试代码:ViewCodeusingSystem;usingSystem.Collections.Generic

使用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{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();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();var smallBlocks new List();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;}}}

 

弱引用如何有效避免OutOfMemoryException异常发生?