File tree Expand file tree Collapse file tree
EnumerableAsyncProcessor.UnitTests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -98,24 +98,23 @@ public async Task TaskWrapper_MemoryFootprint_IsSmallerThanObjects()
9898 {
9999 // This test demonstrates that structs don't allocate on the heap for the wrapper itself
100100 // Only the TaskCompletionSource instances are heap-allocated
101-
101+
102102 // Arrange & Act
103- GC . Collect ( ) ;
104- var initialMemory = GC . GetTotalMemory ( false ) ;
105-
103+ Func < Task > taskFactory = static ( ) => Task . CompletedTask ;
104+ var initialAllocatedBytes = GC . GetAllocatedBytesForCurrentThread ( ) ;
105+
106106 var wrappers = new ActionTaskWrapper [ 1000 ] ;
107107 for ( int i = 0 ; i < wrappers . Length ; i ++ )
108108 {
109109 // Only the TaskCompletionSource allocates on the heap, not the wrapper struct
110- wrappers [ i ] = new ActionTaskWrapper ( ( ) => Task . CompletedTask , new TaskCompletionSource ( ) ) ;
110+ wrappers [ i ] = new ActionTaskWrapper ( taskFactory , new TaskCompletionSource ( ) ) ;
111111 }
112-
113- var finalMemory = GC . GetTotalMemory ( false ) ;
114- var allocatedMemory = finalMemory - initialMemory ;
115-
112+
113+ var allocatedBytes = GC . GetAllocatedBytesForCurrentThread ( ) - initialAllocatedBytes ;
114+
116115 // Assert - Memory allocation should be only for TaskCompletionSource instances
117116 // Each struct itself doesn't allocate heap memory
118- await Assert . That ( allocatedMemory ) . IsLessThan ( wrappers . Length * 200 ) ; // Conservative estimate
117+ await Assert . That ( allocatedBytes ) . IsLessThan ( wrappers . Length * 200 ) ; // Conservative estimate
119118 var hasNonNullFactory = wrappers [ 0 ] . TaskFactory != null ; // Prevent optimization
120119 await Assert . That ( hasNonNullFactory ) . IsTrue ( ) ;
121120 }
You can’t perform that action at this time.
0 commit comments