分析提高GDI編程性能的方法
- IntPtrhandle=font.ToHfont();//性能瓶頸
- //…
- SafeNativeMethods.DeleteObject(handle);
由于該控件在使用GDI畫字時,通過調(diào)用Font.ToHfont()方法獲得Font的Handle。而這個方法非常慢。并且控件在畫每個Item時都被調(diào)用這個方法,F(xiàn)orm中又有很多個這樣的控件,因此調(diào)用次數(shù)相當可觀。這就造成了這個性能瓶頸。
由于操作系統(tǒng)是不允許GDI的Handle個數(shù)大于9999的。如果大于9999個的話,程序就會崩掉。因此,我們絕對不能使程序中GDI的Handle個數(shù)與某些因素有線性增長關系。所有,一般都是在使用GDI畫字時創(chuàng)建Handle,用完之后就刪除掉。這樣也可以防止GDI泄露。
考慮到很多時候,F(xiàn)ont都是相同的,如果能將Font創(chuàng)建的Handle緩存起來,性能就會有很大的提升。但是,緩存的Handle不及時刪除的話,如果Font不相同的太多,就有機會達到操作系統(tǒng)允許的***個數(shù),從而使程序崩潰。
以下是我的提高GDI編程性能解決方案:
1,使用SafeFontHandle類來防止GDI泄露。SafeFontHandle派生自SafeHandleZeroOrMinusOneIsInvalid,而SafeHandleZeroOrMinusOneIsInvalid又派生自CriticalFinalizerObject。GC會對CriticalFinalizerObject做特別處理,保證所有關鍵終止代碼都有機會執(zhí)行。
- Code
- #regionTheSafeFontHandleclass
- internalsealedclassSafeFontHandle:SafeHandleZeroOrMinusOneIsInvalid
- {
- privateSafeFontHandle()
- :base(true)
- {
- }
- publicSafeFontHandle(IntPtrpreexistingHandle,boolownsHandle)
- :base(ownsHandle)
- {
- base.SetHandle(preexistingHandle);
- }
- protectedoverrideboolReleaseHandle()
- {
- returnSafeNativeMethods.DeleteNativeFontHandle(base.handle);
- }
- }
- #endregion
2,使用HandleCollector類防止Font的Handle超過操作系統(tǒng)***限制。HandleCollector會跟蹤Font的Handle,并在其達到指定閥值時強制執(zhí)行垃圾回收。垃圾回收后,SafeFontHandle會釋放Font的handle。
- Code
- [SuppressUnmanagedCodeSecurity]
- internalstaticclassSafeNativeMethods
- {
- privatestaticHandleCollectorFontHandleCollector=newHandleCollector("GdiFontHandle",500,1000);
- internalstaticIntPtrCreateNativeFontHandle(Fontfont)
- {
- IntPtrhandle=font.ToHfont();
- if(handle!=IntPtr.Zero)
- {
- FontHandleCollector.Add();
- }
- returnhandle;
- }
- internalstaticboolDeleteNativeFontHandle(IntPtrhandle)
- {
- boolsuccess=DeleteObject(handle);
- if(success)
- {
- FontHandleCollector.Remove();
- }
- returnsuccess;
- }
- [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
- internalstaticexternboolDeleteObject(System.IntPtrgdiObject);
- }
3,使用弱引用緩存類WeakReferenceCachePool
- Code
- internalstaticclassSafeFontHandleFactory
- {
- #regionInstanceData
- privatestaticWeakReferenceCachePool_cachePool=newWeakReferenceCachePool();
- #endregion
- #regionMethods
- publicstaticSafeFontHandleCreateSafeFontHandle(Fontfont)
- {
- if(font==null)
- {
- thrownewArgumentNullException();
- }
- SafeFontHandlesafeFontHandle=_cachePool[font];
- if(safeFontHandle==null)
- {
- IntPtrnativeHandle=SafeNativeMethods.CreateNativeFontHandle(font);
- safeFontHandle=newSafeFontHandle(nativeHandle,true);
- _cachePool[font]=safeFontHandle;
- }
- returnsafeFontHandle;
- }
- #endregion
- }
這樣就成功的緩存了GDI的Handle,而且在使用完成后,GDI的Handle不會線性增長,只要有GC回收發(fā)生,GDI的Handle都會清零,或者總個數(shù)達到HandleCollector指定的閥值時,也會清零。利用GC垃圾回收機制,在性能和內(nèi)存占用之間自動平衡。
這里是測試代碼,提高GDI編程性能測試如下:
不使用弱引用緩存
TimeElapsed:350ms
CPUCycles:952,061,115
Gen0:1
Gen1:0
Gen2:0
GDIincrement:0
提高GDI編程性能,使用弱引用緩存
TimeElapsed:42ms
CPUCycles:142,020,499
Gen0:0
Gen1:0
Gen2:0
GDIincrement:0
【編輯推薦】