資源簡介
=1,2
垃圾回收算法:
托管堆:CLR要求的資源從托管堆分配,任何對象只要沒有應用程序的根引用它,都會在某個時刻被垃圾回收器回收
基于代的機制,提高回收的性能,在程序的生命期中,新建的對象是新一代,而創建的比較早的對象是老一代,第0代是最近分配的對象,CLR 使用了0,1,2三代進行管理
=3 GC和調試
GcAndDebug.cs
=4本地資源
終結(finalization)是CLR提供的一種機制,允許對象在垃圾回收器回收其內存之前執行一些得體的清理工作,任何包裝了本地資源的類型都必須支持終結操作(實現一個命名為Finalize的方法)。
GC判斷一個對象是垃圾時,會調用對象的Finalize(實現的情況下),C#中使用~ClassName表示Finalize
In most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. Furthermore, the .NET Framework provides a set of prewritten classes derived from the SafeHandle class, and this set is located in the Microsoft.Win32.SafeHandles namespace. These classes are designed to provide common functionality for supporting file and operating system handles.
System.Runtime.ConstrainedExecution 下的 CriticalFinalizerObject 抽象對象,CLR特殊對待
System.Runtime.InteropServices下的派生抽象類
SafeHandle
CriticalHandle 和SafeHandle只是引用計數的區別
Microsoft.Win32.SafeHandles
SafeFileHandle,SafeRegistryHandle ,SafeWaitHandle...
SafeProcessHandle SafeLibraryHandle SafeLocalMemHandle SafeThreadHandle等MSDN沒有編寫,但同樣是處理這個問題的
類似的實現的不同類代表不同的資源類型,這么多的類似,主要是為了類型安全
SafeHandle的作用:
1、以前的IntPtr形式不夠健壯,如在IntPtr賦值前可能拋出ThreadAbortException異常,這樣托管代碼將造成本地資源的泄露
2、防止利用潛在的安全漏洞,一個線程訪問一個本地資源,另一個線程釋放該資源,通過引用計數實現
對托管資源只有在極少數情況下才使用終結器
終結器被調用的時刻:
1、第0代滿:
2、顯式調用GC.Collect
3、Windows報告內存不足
4、CLR卸載AppDomain
5、CLR關閉
內部實現使用終結列表保留對象,通過這個GC進行處理, freachable隊列
=Dispose
終結器的調用時間由GC確定,調用者無法顯式調用它
Dispose提供了顯式清理資源的能力
GCHandle WeakReference
System.Runtime.CompilerServices.ConditionalWeakTable<TKey,TValue>
加入時的是對象的WeakReference,因此可能會回收,但可以確保只要key存在,value也是存在的
任何大于85000字節的對象被自動認為是大對象,大對象在大對象堆中分配
==
大量資源
GC
public static void AddMemoryPressure(Int64 bytesAllocated);
public static void RemoveMemoryPressure(Int64 bytesAllocated);
提示GC實際需要消耗的內存,然后GC監視內存壓力,壓力變大時,強制回收
System.Runtime.InteropServices HandleCollector 數量有限的本地資源
MemoryFailPoint在需要大量內存前可以先檢查是否可以使用這么大的內存
GC的監視
代碼形式:
GC.GetTotalMemory
GC.CollectionCount
PerMon.exe系統性能監視工具
CLR Profiler工具
FUSLOGVW.exe
clrver.exe
SvcTraceViewer.exe
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?GCDemo
{
????internal?sealed?class?GenObj
????{
????????~GenObj()
????????{
????????????Console.WriteLine(“In?Finalize?method“);
????????}
????}
????class?ControlGC
????{
????????public?void?Demo()
????????{
????????????Console.WriteLine(“Maximum?generations:?“?+?GC.MaxGeneration);
????????????//?Create?a?new?GenObj?in?the?heap.??
????????????object?o?=?new?GenObj();
????????????//?Because?this?object?is?newly?created?it?is?in?generation?0.??
????????????Console.WriteLine(“Gen?“?+?GC.GetGeneration(o));?//?0??
????????????//?Performing?a?garbage?collection?promotes?the?object‘s?generation.??
????????????GC.Collect();
????????????Console.WriteLine(“Gen?“?+?GC.GetGene
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1751??2011-04-28?13:31??GCDemo\ControlGC.cs
?????文件????????443??2011-04-28?11:02??GCDemo\DisposeDemo.cs
?????文件????????842??2011-04-28?09:12??GCDemo\GcAndDebug.cs
?????文件???????2812??2011-04-28?13:33??GCDemo\GCDemo.csproj
?????文件???????3172??2011-04-28?13:18??GCDemo\GCOthers.cs
?????文件???????7600??2011-04-28?13:09??GCDemo\GCs.cs
?????文件???????2279??2011-04-28?10:42??GCDemo\NativeResouce.cs
?????文件????????947??2011-04-28?13:33??GCDemo\Program.cs
?????文件???????1442??2011-04-28?08:54??GCDemo\Properties\AssemblyInfo.cs
?????文件???????3594??2011-04-28?15:00??GCDemo\Readme.txt
?????文件????????860??2011-04-28?08:55??GCDemo.sln
?????目錄??????????0??2011-05-13?22:10??GCDemo\Properties
?????目錄??????????0??2011-05-13?22:10??GCDemo
-----------?---------??----------?-----??----
????????????????25742????????????????????13
評論
共有 條評論