File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System . Drawing ;
2+ using Imaging ;
3+
4+ namespace ImagingTests
5+ {
6+ [ TestClass ]
7+ public class ImagingLeakTests
8+ {
9+ [ TestMethod ]
10+ public void TestPixelateForMemoryLeaks ( )
11+ {
12+ // 1. Setup a dummy bitmap
13+ using var testBmp = new Bitmap ( 2000 , 2000 ) ;
14+
15+ // 2. Wrap the execution in our Monitor
16+ LeakDetector . Monitor ( ( ) =>
17+ {
18+ // Run the operation multiple times in one go to amplify any small leak
19+ for ( var i = 0 ; i < 5 ; i ++ )
20+ {
21+ // We simulate the UI usage: Load -> Process -> Convert -> Dispose
22+ using ( var result = ImagingFacade . Resize ( testBmp , 1000 , 1000 ) )
23+ {
24+ var uiImage = ImagingFacade . ToBitmapImage ( result ) ;
25+ // uiImage is now managed by WPF, result is disposed here
26+ }
27+ }
28+ } , "Facade Resize and Convert Leak Test" ) ;
29+ }
30+
31+ [ TestMethod ]
32+ public void TestFilterLeak ( )
33+ {
34+ using var testBmp = new Bitmap ( 1000 , 1000 ) ;
35+
36+ LeakDetector . Monitor ( ( ) =>
37+ {
38+ // Testing if ApplyFilter cleans up its internal DirectBitmap
39+ var filtered = ImagingFacade . ApplyFilter ( testBmp , Imaging . Enums . FiltersType . Sepia ) ;
40+ filtered ? . Dispose ( ) ;
41+ } , "Sepia Filter Disposal Test" ) ;
42+ }
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ using System . Diagnostics ;
2+
3+ namespace ImagingTests
4+ {
5+ public static class LeakDetector
6+ {
7+ // Switch this off in Production
8+ public static bool IsEnabled { get ; set ; } = true ;
9+
10+ public static void Monitor ( Action action , string label , long thresholdBytes = 1024 * 1024 )
11+ {
12+ if ( ! IsEnabled )
13+ {
14+ action ( ) ;
15+ return ;
16+ }
17+
18+ // Clean the slate
19+ GC . Collect ( ) ;
20+ GC . WaitForPendingFinalizers ( ) ;
21+ GC . Collect ( ) ;
22+
23+ long startMem = Process . GetCurrentProcess ( ) . PrivateMemorySize64 ;
24+
25+ action ( ) ;
26+
27+ // Clean up after action
28+ GC . Collect ( ) ;
29+ GC . WaitForPendingFinalizers ( ) ;
30+ GC . Collect ( ) ;
31+
32+ long endMem = Process . GetCurrentProcess ( ) . PrivateMemorySize64 ;
33+ long diff = endMem - startMem ;
34+
35+ if ( diff > thresholdBytes )
36+ {
37+ string msg = $ "[LEAK WARNING] { label } : Memory increased by { diff / 1024.0 / 1024.0 : F2} MB";
38+ Debug . WriteLine ( msg ) ;
39+ // In a Unit Test environment, you might throw an exception here:
40+ // throw new Exception(msg);
41+ }
42+ else
43+ {
44+ Debug . WriteLine ( $ "[OK] { label } : Memory stable (Diff: { diff } bytes).") ;
45+ }
46+ }
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments