0

In a long-running Bash script, I'm calling a custom executable (getIdle) which returns the idle time of the system.

As I have to periodically call this executable, I guess it creates a huge read burden on my hard disk in a long term.

As a solution, I first copy the exe to /dev/shm/ and then run it from there. However, I'm not sure this is a portable solution, so my question comes to this point:

How can I cache an executable in the RAM so that the periodic calls would not be an issue?

1

2 Answers 2

4

I guess it creates a huge read burden on my hard disk in a long term.

No it does not unless you're reading and writing tons of data from/to the disk. Linux uses caching for all read/write operations, so once (having been) run, your binary will be cached and subsequently the kernel will use its image in the cache memory and won't read the file from the disk.

3
  • Even if the kernel caches any tiny binaries for subsequent uses, doesn't it have to check if my binary is modified or not? Wouldn't this require a read from the disk anyway? Or does it have a mechanism to invalidate the cache upon modification (since it has to know when it will be modified)?
    – ceremcem
    Sep 22, 2020 at 14:15
  • 2
    @ceremcem If the binary is modified, the kernel will mark the page as dirty in-memory, since writes also go through the page cache as a writeback. It doesn't need to check the disk every time.
    – Chris Down
    Sep 22, 2020 at 16:03
  • 2
    @ceremcen No, it hasn't have to check. Any write will go through the same unified cache and update the data. No offense intended, but you should find anything else to worry about. Naive caching of data which is already cached is not only pointless, but HARMFUL, because unlike the kernel you don't hold all the cards, and cannot guarantee that you're able to keep it in sync and don't keep duplicating it.
    – user313992
    Sep 22, 2020 at 16:06
-1

You can create tmpfs and copy your executable to it

$ sudo mount -t tmpfs -o size=10M tmpfs /mnt/mytmpfs
3
  • 1
    There's no reason to do this -- Linux will cache the binary. Also, tmpfs can swap, so there's no guarantee there anyway.
    – Chris Down
    Sep 22, 2020 at 12:59
  • 1. tmpfs can swap, but if there is no swap (typical server case) it will stay in RAM. 2. disk cache not only depend on disk R/W operations, but also on RAM usage Sep 22, 2020 at 13:42
  • "No swap" isn't the typical server case, because Linux memory management breaks down without swap since reclaim efficiency is significantly lowered. As for RAM usage: in that respect there's no difference between disk cache and tmpfs, since both can result in becoming non-resident.
    – Chris Down
    Sep 22, 2020 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .