
الـ GC.Collect والـ = null مش هينقذوك: إزاي الـ Garbage Collector في .NET بيشتغل فعلًا
- كاتب موثّق
- .NET 10
محتويات المقال
batch = null; و GC.Collect() في آخر الـ import "عشان الـ memory ترجع أسرع". تاني ليلة الجراف هو هو، والـ import بقى أبطأ، والـ requests اللي بتيجي وقته فيها latency spikes. بعد أسبوعين، heap snapshot لقى السبب: static list شايلة كل batch "عشان صفحة الـ status". ومهما عملت null أو collection، مش هتحرّر memory لسه عليها reference.
الفكرة الغلط الشائعة
null أول ما تخلص، الـ memory هترجع أسرع. ولو الـ memory لسه شكلها عالي، GC.Collect() بينضّف ويخلّي الـ app أسرع.
x = null شكلها النسخة الـ managed من delete. والـ demos بتثبت ده ظاهريًا: نادي GC.Collect() تلاقي GC.GetTotalMemory نزل. والـ Task Manager بيوريك رقم كبير مش بيصغر لوحده.
اللي بيحصل فعلًا
الـ GC بيدوّر على العايش مش على اللي مات
الـ collections بتبدأ بسبب الـ allocations
GC.Collect(). والـ null مش بيبدأ أي حاجة. وعن السبب التالت الـ docs صريحة: في كل الحالات تقريبًا مش محتاج تناديها، لأن الـ GC شغال على طول.
الـ generations بتخلّي أغلب الـ collections رخيصة
الـ = null بتعمل إيه فعلًا؟
= null بتعمل إيه فعلًا؟x = null في آخر الـ method غالبًا دوشة. الـ null بتفرق بس لما تشيل آخر طريق من root عايش طويل: static field، أو singleton، أو cache entry، أو event subscription. وده برضه مكان الـ leaks الحقيقية.
الـ GC.Collect() بتعمل إيه فعلًا؟
GC.Collect() بتعمل إيه فعلًا؟GC.Collect() من غير parameters بتعمل collection كاملة وblocking لكل الـ generations، والـ managed threads بتقف وهي شغالة، ومعاهم اللي بيخدموا الـ requests. وأي object لسه عايش ساعتها، زي داتا الـ requests اللي شغالة، بيترقّى لـ generation أقدم، بيتعمله collection أقل وبتكلفة أعلى. ونصيحة Microsoft لـ ASP.NET Core واضحة: بشكل عام، الـ apps في الـ production ماتنادهاش.
الـ import بعد الـ fix
public static class Importer{ // Kept "for the status page": every batch stays reachable forever private static readonly List<List<OrderRow>> s_history = []; public static void Import(IEnumerable<OrderRow> rows) { List<OrderRow>? batch = rows.ToList(); Save(batch); s_history.Add(batch); // the real reason memory never comes back batch = null; // changes nothing: s_history still holds it GC.Collect(); // a full blocking GC that frees nothing } static void Save(List<OrderRow> batch) { /* write to the database */ }}public static class Importer{ // The status page only needs a summary, not the rows public static ImportSummary? LastImport { get; private set; } public static void Import(IEnumerable<OrderRow> rows) { var batch = rows.ToList(); Save(batch); LastImport = new(batch.Count, DateTimeOffset.UtcNow); // No null, no GC.Collect: once Import returns, nothing reaches // the batch, and a later collection reclaims it when space is needed } static void Save(List<OrderRow> batch) { /* write to the database */ }}public record ImportSummary(int Rows, DateTimeOffset At);batch = null بتمسح local بس والـ list لسه جوه s_history، وكل GC.Collect() بيوقف الـ service عشان يتأكد إن كل حاجة لسه يتوصلها. في التانية، مفيش حاجة عايشة طويل بتشاور على الـ rows، فبتبقى garbage أول ما Import ترجع.
الفكرة الغلط vs الحقيقة
null بيحرّر الـ object | |
GC.Collect() بيخلّي الـ app أسرع | |
اقرا جرافات الـ memory صح
تطبقها إزاي في مشروعك
امسح نداءات GC.Collect شيل GC.Collect()من مسارات الكود اللي في الـ production. ولو واحد لازم يفضل، اكتب السبب اللي بيحصل مرة واحدة، وقيس إنه بيساعد فعلًا.بطّل تعمل null للـ locals امسح سطور x = nullللـ locals والـ parameters. وخلّي التنضيف الصريح للحاجات اللي عايشة طويل بس: امسح الـ cache entries، وشيل الـ event handlers، وطلّع العناصر من الـ static collections.حط حدود للي عايش طويل ادّي الـ caches حد للحجم أو مدة expiration، وخزّن ملخصات بدل الداتا كاملة في أي حاجة static أو singleton. قيس قبل ما تظبط راقب حجم الـ GC heap وعدد الـ gen 2 collections مع الوقت بـ dotnet-counters، وغيّر إعدادات الـ GC بس لما الأرقام تشاور على مشكلة حقيقية.
الخلاصة
الـ GC بيرجّع الـ objects اللي مفيش root يوصلها، في collections بتبدأ بسبب الـ allocations والضغط على الـ memory. الـ = nullعلى local نادرًا ما بتفرق، واللي بيحرّر الـ memory هو إنك تشيل آخر reference من static أو cache أو event.الـ GC.Collect()بيجبر collection كاملة blocking وبيرقّي الـ objects العايشة، فخلّيه برّه كود الـ production.الـ memory العالية مع الـ Server GC مش leak لوحدها، الـ leak هو memory عمرها ما بتثبت. عشان تصلّح leak، دوّر على الـ root اللي ماسك الـ objects بدل ما تجبر collections.
التعليقات (0)