Slow performance when copying files to and from USB devices

There seems to be a problem with huge pages in linuxes memory management. It rarely occurs, but sounds like you have observed it.

Cause

This is my grossly simplified account of what, according to the article, happens.

If unlucky, a process gets stuck the moment it issues a memory access. That's because when transparent huge pages is enabled, a memory access may trigger synchronous compaction (defragmentation of main memory,) synchronous meaning the memory access does not finish before the compaction does. This in itself is not a bad thing. But if write-back (of, e.g., buffered data to USB) happens to take place at the same time, compaction in turn is likely to stall, waiting for the write-back to finish.

So, any process could end up waiting for a slow device to finish writing buffered data.

Cure

Upgrading main memory, as the OP did, might help delay the problem. But for those who don't consider that an option, there are two obvious workarounds. Both involve recompiling the kernel:

  • disabling the transparent huge pages feature
  • applying Mel's patch as mentioned in the article

This sounds similar to my question here (where an answer pointed me to this question):

https://stackoverflow.com/questions/10105203/how-can-i-limit-the-cache-used-by-copying-so-there-is-still-memory-available-for

But the theory is completely different, and the solution I used is unrelated to yours, but works perfectly.

I was using rsync, so all I had to do was use the --drop-cache option. (which makes the copy a bit slower as a side effect)