Reputation: 1780
I'm curious as to what memory resource is used when reading a file. Where is block stored? Is it the heap and thus the RAM?
with open("file.txt", "r") as fd:
block = fd.read(64)
Upvotes: 4
Views: 111
Reputation: 1972
The memory block (not the same as the block
variable) is probably stored in RAM at some point, but not in your process heap. The operating system does some very complicated stuff (look info Virtual Memory for more on this) to give each process its own section of memory. The OS kernel also takes up a very big section.
Now, read
is a system call, meaning that your processor hands the steering wheel over to the kernel to do its stuff. The kernel then handles copying from disk to memory in whatever way it wants, but only the data you request will be stored in your process's chunk of memory. In your example, the kernel would somehow fetch the block, probably storing it somewhere in RAM (but it will also utilize processor caches to allow for faster access later), but the only guarantee is that up to 64 bytes of it will be stored in a variable on the heap (pointed to by the name block
).
Upvotes: 4