• Ethan@programming.dev
    link
    fedilink
    English
    arrow-up
    3
    ·
    2 days ago

    Garbage collection is analyzing the heap and figuring out what can be collected. Reference counting requires the code to increment or decrement a counter and frees memory when the counter hits zero. They’re fundamentally different approaches. Also reference counting isn’t necessarily automatic, Objective-C had manual reference counting since day one.

    • BatmanAoD@programming.dev
      link
      fedilink
      arrow-up
      6
      ·
      2 days ago

      “Garbage collection” is ambiguous, actually; reference counting is traditionally considered a kind of “garbage collection”. The type you’re thinking of is called “tracing garbage collection,” but the term “garbage collection” is often used to specifically mean “tracing garbage collection.”

    • CanadaPlus@lemmy.sdf.org
      link
      fedilink
      arrow-up
      3
      ·
      2 days ago

      It’s still mentioned as one of the main approaches to garbage collection in the garbage collection Wikipedia article.

      • Ethan@programming.dev
        link
        fedilink
        English
        arrow-up
        1
        ·
        18 hours ago

        Ok, I concede the point, “garbage collection” technically includes reference counting. However the practical point remains - reference counting doesn’t come with the same performance penalties as ‘normal’ garbage collection. It has essentially the same performance characteristics of manual memory management because that’s essentially what it’s doing.

        • CanadaPlus@lemmy.sdf.org
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          17 hours ago

          That may well be. I’d say I understand the basic concepts, but people in this thread have more detail on the specifics and how they work out in practice than me.

          It does make me wonder why everyone hasn’t been doing it, if there’s no drawbacks, though.

          • Ethan@programming.dev
            link
            fedilink
            English
            arrow-up
            1
            ·
            5 hours ago

            It is being used. Objective-C (used for macOS and iOS apps) has used reference counting since the language was created. Originally it was manual, but since 2011 it’s been automatic by default. And Swift (which basically replaced Objective-C) only supports ARC (does not support manual reference counting). The downside is that it doesn’t handle loops so the programmer has to be careful to prevent those. Also, the compiler has to insert reference increment and decrement calls, and that’s a significant engineering challenge for the compiler designers. Rust tracks ownership instead of references, but that means it’s compiler is even more complicated. Rust’s system is a little bit like compile-time reference counting, but that’s not really accurate. Apparently Python, Pearl, and PHP use reference counting, plus tracing GC (aka ‘normal’ GC) in Python and PHP to handle cycles. So your implicit statement/assumption that reference counting is not widely used is false. Based on what I can find online, Python and JavaScript are by far the most used languages today and are roughly equal, so in that respect reference counting GC is equally or possibly more popular than pure tracing GC.