How to AVOID Memory Leaks in your C# .net application

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 8

  • @dsfgato
    @dsfgato 2 месяца назад

    excelent video

  • @arthell1393
    @arthell1393 5 месяцев назад

    Great video! Didn`t know about the event listener example

  • @3DMage
    @3DMage 2 месяца назад

    In the second example with the 'static' keyword, it seems to me that when LongLivedLeak() finishes execution, unless 'instance' is stored elsewhere in the program (like returning it or it is assigned elsewhere to a variable outside the function), then 'instance' would essentially be garbage collected due to the function going out of scope when it is done. I can see 'instance' not being garbage collect if that instance was assigned elsewhere and never handled with properly.

  • @alejandroguardiola9085
    @alejandroguardiola9085 5 месяцев назад

    I am confused with the second example, is my understanding that the instance will be mark for collection after the function finish, as the variable that holds the pointer is in the stack.

    • @Mark-D-Inman
      @Mark-D-Inman  5 месяцев назад

      I totally agree that it is confusing, but it works like this.
      The static method itself "LongLivedLeak" is never garbage collected because it's loaded into memory along with the class definition and remains available throughout the program's execution. Even though the local variable "instance" itself might be eligible for garbage collection after the loop, the problem is that the static "LongLivedLeak" method still holds a reference to it. This indirect reference prevents the garbage collector from reclaiming the memory occupied by the instance object and its growing data list.

    • @alejandroguardiola9085
      @alejandroguardiola9085 5 месяцев назад

      @@Mark-D-Inman I understand the method is never collected because it is static, but everything created and not returned in a single execution will be collected, the method will never hold the instance that would be catastrophic I would think if this method is called in a loop.

    • @alejandroguardiola9085
      @alejandroguardiola9085 5 месяцев назад

      @@Mark-D-Inman A long lived leak I think would be if a static class holds a reference to the instance directly or indirectly inside a property but not a method.

  • @rohanar-qz6fn
    @rohanar-qz6fn 5 месяцев назад

    Nice ✨️ 😊