Commit 43ad43e
committed
fix(memory): add GC memory pressure tracking for native allocations
Fixes GitHub issue #501 where memory would grow to 10GB+ when creating
many NDArrays in a loop without explicit GC.Collect() calls.
Root cause: NumSharp allocates data via NativeMemory.Alloc (unmanaged)
but did not inform the GC about this memory pressure. The GC only saw
small managed wrapper objects (~100 bytes) and was unaware of the
~880+ bytes of unmanaged data per array, so it wouldn't trigger
collections frequently enough.
Fix: Add GC.AddMemoryPressure() when allocating unmanaged memory and
GC.RemoveMemoryPressure() when freeing it. This informs the GC about
the true memory footprint so it schedules collections appropriately.
Only the Native path (NativeMemory.Alloc) tracks pressure. External
memory paths (np.frombuffer with dispose) are the caller's responsibility.
Before fix: Creating 1M arrays with 110 doubles each peaked at 10+ GB
After fix: Same workload peaks at ~54 MB (stable, proper GC behavior)1 parent 2fc4420 commit 43ad43e
1 file changed
Lines changed: 22 additions & 6 deletions
Lines changed: 22 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | | - | |
58 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
64 | | - | |
| 67 | + | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
| |||
1017 | 1020 | | |
1018 | 1021 | | |
1019 | 1022 | | |
1020 | | - | |
1021 | | - | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
1022 | 1030 | | |
1023 | 1031 | | |
| 1032 | + | |
1024 | 1033 | | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
1025 | 1038 | | |
1026 | 1039 | | |
1027 | 1040 | | |
| |||
1052 | 1065 | | |
1053 | 1066 | | |
1054 | 1067 | | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
1055 | 1071 | | |
1056 | 1072 | | |
1057 | 1073 | | |
| |||
0 commit comments