- When i do this : mf.open(params) , i see that a file is created on disk with size 100. Now when i write to it i.e *w = w1, the contents of the file on disk changes. Does this mean that i am not using the RAM at all and i am writing straight into the disk?
You're using memory mapped files. This means both: you are writing to 'virtual memory pages' that have been mapped into your process space, but actually refer to disk blocks. The growth indicates that the pages get committed on write.
- When i do mf.size(), it always give me the size which i gave as the input for creating the actual file. How can i find out the size of the data that i actually wrote into the memory mapped file?
You can't. You can only find the number of blocks committed with a tool like stat
- Also if i give params.new_file_size = 10GB, the file of that size gets created on the disk but it does not take up any disk space.Confirmed by using df cmd. Why so? -rwx------. 1 root root 10000000000 Apr 29 14:26 map.dat
It's sparsely allocated. E.g. using fallocate
or similar on other platforms.
- I read that close file frees the mapping. Does this mean that after close i lose all the data that i wrote? But this is not true as i have the working code where i close and then open the file again and read it correctly.
No. It means that the mapping is freed. That is, the /virtual memory/ area in your process space is now 'free' to use for other things.
- How to delete the memory mapped files created after use? By using rm -rf cmd/linux apis?
Yes.