Caching for the du command

I’ve written a directory scanner in c++ that utilizes the ‘du --summarize’ command. After a bunch of hacking everything appears to be working the way it should. The caveat is that I know ‘du’ is caching information somewhere because the scan is taking far too little time to complete (it scans about 1TB of information in a second or two). More importantly when I first started tinkering with this project ‘du’ was taking longer from both cli and from the application. Does anyone know any easy sure-fire way to set everything back to zero (clear the necessary caches) so I can test performance and behavior?

First, test it:

time du -ch

Try running the following to drop cache:

sudo sync
sudo sync
echo 3 | sudo tee /proc/sys/vm/drop_caches 
# Turn off swap
sudo swapoff -a

Read more about it by typing the following:

man swapoff
man 5 proc

Now test again:

time du -ch

Thanks Vivek, your knowledge of Linux is encyclopedic!!!

@Ian_King you are welcome.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki