Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files.write slow on Windows Surface

I have a simple Java 17 method, that writes content to a specified path. The content is not long, just around 1kb.

It in general works as expected, but on a specific device, a Windows 11 Surface Pro 7, this write operation can take 5-10 seconds!

Path path = Paths.get(file);
Files.createDirectories(path.getParent());
try {
  byte[] bytes = Base64.getDecoder().decode(data.getBytes());
  Files.write(path, bytes);
} catch /* ... */

It seems it gets slowed down by accessing the file system of that device.

  • What could be the reason for that?
  • What actions could be done to increase the file system access?

Edit:

Added output form winsat

C:\Windows\System32>winsat disk -drive c
Windows-Systembewertungstool
> Wird ausgeführt: Featureaufzählung ''
> Laufzeit 00:00:00.00
> Wird ausgeführt: Speicherbewertung '-drive c -ran -read'
> Laufzeit 00:00:00.67
> Wird ausgeführt: Speicherbewertung '-drive c -seq -read'
> Laufzeit 00:00:02.78
> Wird ausgeführt: Speicherbewertung '-drive c -seq -write'
> Laufzeit 00:00:02.13
> Wird ausgeführt: Speicherbewertung '-drive c -flush -seq'
> Laufzeit 00:00:00.77
> Wird ausgeführt: Speicherbewertung '-drive c -flush -ran'
> Laufzeit 00:00:00.72
> Dshow-Videocodierzeit                        0.00000 s
> Dshow-Videodecodierzeit                      0.00000 s
> Media Foundation-Decodierzeit                0.00000 s
> Disk  Random 16.0 Read                       142.87 MB/s          7.4
> Disk  Sequential 64.0 Read                   1747.81 MB/s          8.9
> Disk  Sequential 64.0 Write                  902.61 MB/s          8.4
> Durchschnittliche Lesezeit mit sequenziellen Schreibvorgängen0.232 ms          8.5
> Latenz: 95. Perzentil                        0.398 ms          8.7
> Latenz: Maximum                              8.509 ms          8.1
> Durchschnittliche Lesezeit bei zufallsgesteuerten Schreibvorgängen0.224 ms          8.9
> Gesamtausführungszeit 00:00:07.42
like image 520
MoxxiManagarm Avatar asked Apr 30 '26 01:04

MoxxiManagarm


1 Answers

This is unlikely to be a problem that is Java specific. Your example is simply create and writing a file. While there is some work in this like:

byte[] bytes = Base64.getDecoder().decode(data.getBytes());

that is all well-optimized code, and it won't depend on the platform in any significant way. It will take milliseconds at most.

The code where the time is being spent will be these lines:

Files.createDirectories(path.getParent());

and

Files.write(path, bytes);

But the vast majority of time taken for those two lines will be consumed in the operating system.


From other peoples comments, there are a couple of plausible theories about what could be going on:

  1. Your SSD is dying and the OS is struggling to get disk writes to succeed. (The problem could be only for specific SSD disk blocks; e.g. the blocks that hold the metadata for the directory you are creating the file in. They may not show up in a disk performance test.)

  2. Something else on the system is interfering; e.g. anti-virus software.

Your comment seems to support this.

The code works fine and fast on all devices. The only slow device is that single one. No exceptions occur, they are all catched. It is a performance issue only.


Anish B. wrote

The issue could be the JDK that you are using from a specific vendor which is not able to use the system architecture properly and might be causing the latency.

As far as I am aware, all vendors for Java on Windows started with the OpenJDK codebase. It is hard to see how they could have gotten basic file I/O to perform so poorly, let alone to have not have picked this up in testing. IMO, changing Java vendors would be a waste of time.

And as far as Java (hypothetically) needing to do something special to make file I/O fast on the platform, it is really the host operating system's job to do that. It is hard to see how the Java runtime could do anything about it ... if the operating system itself can't.

like image 158
Stephen C Avatar answered May 01 '26 14:05

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!