9

I was reading about how you can use the java nio library to take advantage of file transfer/buffering at the O/S level which is called 'zero copy'.

What are the differences in how you create/write to files then? Are there any drawbacks to using zero-copy?

loyalflow
  • 14,275
  • 27
  • 107
  • 168

2 Answers2

8

zero copy means that your program will not transfer the data from the kernel space to the user space and so on. this is faster nice article can be found here:

https://developer.ibm.com/articles/j-zerocopy/

Vishrant
  • 15,456
  • 11
  • 71
  • 120
miks
  • 514
  • 2
  • 5
  • 5
    I read the article and understand the benefits of zero copy but as a java developer how do you verify (through debugging or monitoring) that the data does not pass through the application when using the transferTo() method call? Your thoughts would be appreciated. – Andy Dufresne Mar 18 '13 at 06:52
  • It just works. Plain and simple. When you call the `transferTo()` method Java automatically transfers the data in the _zero-copy_ fashion. Since you are *not* using traditional `read` and `write` calls, the data is transferred at the OS level. If you're still curious, compare the execution speed against a traditional loop. – veganaiZe Sep 12 '17 at 01:48
  • 1
    the link has moved to https://developer.ibm.com/articles/j-zerocopy/ – Ilam Aug 16 '22 at 14:38
6

Zero copy is a technique where the application is no longer the 'middleman' in transferring data from a disk to the socket. Applications that use zero copy request that the kernel copy the data directly from the disk file to the socket, without going through the application, which improves performance and reduces context switches.

It all depends on what the application will do with the data it reads from disks. If it is a web application serving a lot of static content by reading files and relaying them over sockets, then zero copy is the way to go in order to get better performance. However, if the application is using the data locally (either crunching it in some way and then writing it back, or displaying it locally without persisting it back), you would not use zero copy.

This IBM DeveloperWorks article about zero copy is a good read.

Other ways of file I/O in java are via the use of Stream classes based on the type of file you would want to read/write. This involves both buffered and unbuffered streams, although usually buffered streams promise better performance since they cause less I/O seek cycles and hence lesser context switches.

Nagendra U M
  • 601
  • 2
  • 6
  • 16
  • Like if the application is taking a stream from a http request and saving it to disk, which is 'better'? – loyalflow May 11 '12 at 15:37