• OsrsNeedsF2P@lemmy.ml
      link
      fedilink
      arrow-up
      10
      ·
      7 months ago

      Back when I was a wee bit Java noob, I was trying to write a RuneScape bot to play Soul Wars. I had a basic recursive pathfinding algo for figuring out how to walk around the map, but it blew out of memory very quickly (each tile has 4 options, do that recursively, etc). So I added caching. Anyways, I never cleared the caching. So after 20 minutes of running the script, you had like 2GB of allocated RAM calculating the best path from any 2 tiles in the minigame.

      Great times. No amount of language safety features would have saved me from that stupidity.

    • flashgnash@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      7 months ago

      Thought the whole point was that it forced you to handle memory properly and automatically released things when they go out of scope

      What kind of situation can cause a memory leak in rust

      • naonintendois@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        7 months ago

        You can have a memory leak when items are still in scope in some loop or when you have a reference count cycle. The latter happens with the Rc/Arc types in rust.

        An example for the former can be a web server that keeps track of every request it’s ever received in memory. You will eventually run out of memory. But you did not violate any memory rules (dangling pointer, etc.). Memory leaks can be caused by design issues.

        • flashgnash@lemm.ee
          link
          fedilink
          arrow-up
          0
          ·
          7 months ago

          That doesn’t fit the definition of memory leak in my mind, had thought a memory leak was specifically when the program completely loses track of memory

          • MoonMelon@lemmy.ml
            link
            fedilink
            English
            arrow-up
            0
            arrow-down
            1
            ·
            edit-2
            7 months ago

            That’s one kind, and Rust’s “ownership” concept does mean there’s built-in compile time checks to prevent dangling pointers or unreachable memory. But there’s also just never de-allocating stuff you allocated even though it’s still reachable. Like you could just make a loop that allocates memory and never stops and that’s a memory leak, or more generally a “resource leak”, if you prefer.

            Rust is really good at keeping you from having a reference to something that you think is valid but it turns out it got mutated way down in some class hierarchy and now it’s dead, so you have a null pointer or you double free, or whatever. But it can’t stop the case where your code is technically valid but the resource leak is caused by bad “logic” in your design, if that makes sense.