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

  2 Responses to “Way not to use Cache in Linux”

  1. Introducing the way not to use cache in linux programming. http://bit.ly/7VJyxM

  2. Introducing the way not to use cache in linux programming. http://bit.ly/7VJyxM

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   

 

December 2009
S M T W T F S
« Nov   Feb »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Follow Me

© 2011 Bread & Cup Suffusion theme by Sayontan Sinha