This is a relief to find! I just looked at htop and panicked over the high amount of “used” memory.

    • sasalzig
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      As I said, every file read from disk, be it an executable, image or whatever gets cached in RAM automatically and always.

      Having said that, if you read a file using read(2) (or like any API that uses read() internally, which is most), then you end up with two copies of the file in RAM, the version your OS put in the disk cache, and the copy you created in your process’s memory. You can avoid this second copy by using mmap(2). In this case the copy of the file in the disk cache gets mapped into your process’s memory, so the RAM is shared between your copy and the disk cache copy.

      You can also give hints to the disk cache subsystem in the kernel using fadvise(2). Don’t though unless you know what you’re doing.