# Force Linux to cache writes to USB disk?

## arrummzen

I have 400MB of free RAM, yet any time I edit or write data in a file, my USB hard spins up.

Can I somehow tell Linux to maybe, use some of that 400MB of *free* memory as write cache. I think it does this on machines with internal IDE disks, but it doesn't for USB? There is a common saying that "free memory is wasted memory". I really think that is the case here, Linux is wasting 400MB of RAM and abusing (shortening the lifespan of my USB hard drive).

Thank you for your time,

Arrummzen

----------

## NeddySeagoon

arrummzen,

How do you mount your USB partitions?

Show us the command and/or your /etc/fstab entries.

There are two issues here, the existence of a cache in the first place and how long data is allowed to remain there before its committed to disk. 

To get cacheing, which is the defualt, be sure you do not use the sync option. (nosync is defualt).

How long data is allowed to remain in the cache varies from filesystem to filesystem but its typically only a few seconds.

The cache is not flushed - it becomes disk buffer, so a read following the write is satisfied from the cache.

----------

## Mark Clegg

I had a similar issue with the external USB drive I use for backups.

I don't have the device listed in fstab, as I use udev/pmount etc.

Turned out that if I mounted it from the command line, I got 13M/sec transfer rates, but if I mounted it from the device icon in KDE, I got significantly lower.

Right clicking the device icon on the desktop, selecting "Properties" and the "Mountin" tab showed the "synchronous" option selected by default. Unticking it resolved the issue.

----------

## StringCheesian

To explicitly force full caching you can copy the data into a tmpfs before working with it, then copy it back when you're done.

Maybe the filesystem you're using only caches some of it, like the actual content of the files, and other stuff gets written immediately, like journaling data or something.

----------

## arrummzen

I mount it with

mount -o commit=180,noatime /dev/sda1 /

It is an ext3 filesystem...

Is it possible that Linux defaults to sync for USB filesystems?

Thank you for your time,

Arrummzen

----------

## NeddySeagoon

arrummzen,

You can test for sync and nosync and see the difference.

You need a large file to write to the drive. Anything over 100Mb will do, the idea is to use a file that is too big to fit into the on drive buffers. You can create a test file with 

```
dd if=/dev/zero of=test.file bs=4096 count=2500
```

Mount the drive using the sync option and run

```
time cp test.file /path/to/drive/test.file1 
```

Repeat with the drive mounted with the nosync option

Expect to see a difference of a factor of 10 or more in the times. 

I'm fairly sure that nosync is the defualt.

The commit= option may only set an upper limit, commits may be made sooner if CPU time permits. You could lose a lot of data with a 3 minute commit interval too.

----------

