Here is an information not to use cache in linux while reading the kernel newbie mailing list.
Hi!
On 19:29 Fri 04 Dec , Lal wrote:
> Dear list members,
>
> I have a question about Linux page cache. I create a 10MB file using
> following application code:
>
> int fd=open(“foo.txt”, O_CREAT|O_RDWR|O_SYNC);
> char* content=(char *)malloc(1024*1024*10);
> write(fd, content, 1024*1024*10);
> free(content); close(fd);
>
> Immediately before and after this code execution, command “free -m”
> shows free memory decreased by 10M and cache incresed by 10M.
>
> If I delete foo.txt, then free memory increases by 10M and cache
> decreases by 10M.
>
> My question is why the cache is growing even after O_SYNC flag? Even
> fsync does not help. But deleting file freeing the cache.O_SYNC does not mean “do not cache”. It means “make sure the data is written
to disk. The data is kept in memory even after writing to speed up subsequent
reads. If you do not want this, you can pass the O_DICECT flag. But be aware
that using this can easily slow you down.
Either way, if you want to write zeros to the file, make sure the buffer is
initialised after malloc, e.g. via memset or bzero. If you only want to
allocate a file with zeros, you can also consider using fallocate or sparse
files.-Michi