Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 176135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:49:22+00:00 2026-05-11T13:49:22+00:00

How can data written to a file really be flushed/synced with the block device

  • 0

How can data written to a file really be flushed/synced with the block device by Java.

I tried this code with NIO:

FileOutputStream s = new FileOutputStream(filename) Channel c = s.getChannel() while(xyz)     c.write(buffer) c.force(true) s.getFD().sync() c.close() 

I supposed that c.force(true) togehter with s.getFD().sync() should be sufficient because the doc for force states

Forces any updates to this channel’s file to be written to the storage device that contains it. If this channel’s file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device. This is useful for ensuring that critical information is not lost in the event of a system crash.

The documentation to sync states:

Force all system buffers to synchronize with the underlying device. This method returns after all modified data and attributes of this FileDescriptor have been written to the relevant device(s). In particular, if this FileDescriptor refers to a physical storage medium, such as a file in a file system, sync will not return until all in-memory modified copies of buffers associated with this FileDesecriptor have been written to the physical medium. sync is meant to be used by code that requires physical storage (such as a file) to be in a known state.

These two calls should be sufficient. Is it? I guess they aren’t.

Background: I do a small performance comparison (2 GB, sequential write) using C/Java and the Java version is twice as fast as the C version and probably faster than the hardware (120 MB/s on a single HD). I also tried to execute the command line tool sync with Runtime.getRuntime().exec(‘sync’) but that hasn’t changed the behavior.

The C code resulting in 70 MB/s is (using the low level APIs (open,write,close) doesn’t change much):

FILE* fp = fopen(filename, 'w'); while(xyz) {     fwrite(buffer, 1, BLOCK_SIZE, fp); } fflush(fp); fclose(fp); sync(); 

Without the final call to sync; I got unrealistical values (over 1 GB aka main memory performance).

Why is there such a big difference between C and Java? There are two possiblities: I doesn’t sync the data correctly in Java or the C code is suboptimal for some reason.

Update: I have done strace runs with ‘strace -cfT cmd’. Here are the results:

C (Low-Level API): MB/s 67.389782

 % time     seconds  usecs/call     calls    errors syscall ------ ----------- ----------- --------- --------- ----------------  87.21    0.200012      200012         1           fdatasync  11.05    0.025345           1     32772           write   1.74    0.004000        4000         1           sync 

C (High-Level API): MB/s 61.796458

 % time     seconds  usecs/call     calls    errors syscall ------ ----------- ----------- --------- --------- ----------------  73.19    0.144009      144009         1           sync  26.81    0.052739           1       65539           write 

Java (1.6 SUN JRE, java.io API): MB/s 128.6755466197537

 % time     seconds  usecs/call     calls    errors syscall ------ ----------- ----------- --------- --------- ----------------  80.07  105.387609        3215     32776           write   2.58    3.390060        3201      1059           read   0.62    0.815251      815251         1           fsync 

Java (1.6 SUN JRE, java.nio API): MB/s 127.45830221558376

   5.52    0.980061      490031         2           fsync   1.60    0.284752           9     32774           write   0.00    0.000000           0        80           close 

The time values seem to be system time only and are therefore pretty meaningless.

Update 2: I switched to another server, rebooted, and I use a fresh formatted ext3. Now I get only 4% differences between Java and C. I simply don’t know what went wrong. Sometimes things are strange. I should have tried the measurement with another system before writing this question. Sorry.

Update 3: To summarize the answers:

  • Use c.force(true) followed by s.getFD().sync() for Java NIO and s.flush() and s.getFD().sync() for Java’s stream API. For the High-Level API in C don’t forget to sync. A fflush submitted the data to the OS, but doesn’t bring your data to the block device.
  • Use strace to analyze the syscalls done by a command
  • Cross check your results before posting a question.

Update 4: Please note the following follow-up question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:49:23+00:00Added an answer on May 11, 2026 at 1:49 pm

    You need to tell us more about the hardware and operating system, also the specific Java version. How are you measuring this throughput?

    You’re correct that force/sync should force the data out to the physical media.


    Here’s a raw version of copy. Compiled with gcc 4.0 on an Intel Mac, should be clean.

    /* rawcopy -- pure C, system calls only, copy argv[1] to argv[2] */  /* This is a test program which simply copies from file to file using  * only system calls (section 2 of the manual.)  *  * Compile:  *  *      gcc -Wall -DBUFSIZ=1024 -o rawcopy rawcopy.c  *  * If DIRTY is defined, then errors are interpreted with perror(3).  * This is ifdef'd so that the CLEAN version is free of stdio.  For  * convenience I'm using BUFSIZ from stdio.h; to compile CLEAN just  * use the value from your stdio.h in place of 1024 above.  *  * Compile DIRTY:  *  *      gcc -DDIRTY -Wall -o rawcopy rawcopy.c  *  */ #include <fcntl.h> #include <sys/types.h> #include <sys/uio.h> #include <stdlib.h> #include <unistd.h> #if defined(DIRTY) #   if defined(BUFSIZ) #       error 'Don't define your own BUFSIZ when DIRTY' #   endif #   include <stdio.h> #   define PERROR perror(argv[0]) #else #   define CLEAN #   define PERROR #   if ! defined(BUFSIZ) #       error 'You must define your own BUFSIZ with -DBUFSIZ=<number>' #   endif #endif  char * buffer[BUFSIZ];          /* by definition stdio BUFSIZ should                                    be optimal size for read/write */  extern int errno ;              /* I/O errors */  int main(int argc, char * argv[]) {     int fdi, fdo ;              /* Input/output file descriptors */     ssize_t len ;               /* length to read/write */     if(argc != 3){         PERROR;         exit(errno);     }      /* Open the files, returning perror errno as the exit value if fails. */     if((fdi = open(argv[1],O_RDONLY)) == -1){         PERROR;         exit(errno);     }     if((fdo = open(argv[2], O_WRONLY|O_CREAT)) == -1){         PERROR;         exit(errno);     }      /* copy BUFSIZ bytes (or total read on last block) fast as you        can. */     while((len = read(fdi, (void *) buffer, BUFSIZ)) > -1){         if(len == -1){             PERROR;             exit(errno);         }         if(write(fdo, (void*)buffer, len) == -1){             PERROR;             exit(errno);         }     }     /* close and fsync the files */     if(fsync(fdo) ==-1){         PERROR;         exit(errno);     }     if(close(fdo) == -1){         PERROR;         exit(errno);     }     if(close(fdi) == -1){         PERROR;         exit(errno);     }      /* if it survived to here, all worked. */     exit(0); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's called the "null coalescing operator" and works something like… May 11, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer Perhaps if you wrote an attached property for your Validator… May 11, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer This doesn't exactly "automatically fix anything" but there is a… May 11, 2026 at 5:50 pm

Related Questions

I've just been tasked by my boss to implement in a week a way
The program is supposed to accept telnet connections on port 8888 and then send
I'm currently trying to debug a customer's issue with an FTP upload feature in
I'm currently working on cross-platform applications and was just curious as to how other

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.