JDK-8366761 : (zipfs) Files.newByteChannel against a ZipFS Path can lead to OutOfMemoryError
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 17
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2025-09-03
  • Updated: 2025-09-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Causes :  
Description
(Originally uncovered by Volkan [~vyazici])

The following code:

final Path zipFile = ...; // Path to a ZIP file
final URI zipFSURI = URI.create("jar:file:" + zipFile.toAbsolutePath());
// open it using a FileSystem
try (FileSystem zipfs = FileSystems.newFileSystem(zipFSURI, Map.of())) {
    // now create a ByteChannel out of that large entry
    ByteChannel bc = Files.newByteChannel(zipfs.getPath(ENTRY_NAME), Set.of());
...

for a Path that represents a large entry in a ZIP file, can lead to an OutOfMemory error like:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.base/java.io.InputStream.readNBytes(InputStream.java:407)
    at java.base/java.io.InputStream.readAllBytes(InputStream.java:348)
    at jdk.zipfs/jdk.nio.zipfs.ZipFileSystem.newByteChannel(ZipFileSystem.java:952)
    at jdk.zipfs/jdk.nio.zipfs.ZipPath.newByteChannel(ZipPath.java:857)
    at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.newByteChannel(ZipFileSystemProvider.java:244)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:357)
    at TestZipFS.main(TestZipFS.java:48)

This is because of the implementation in ZipFileSystem which reads into memory, the entire content of the ZIP entry when the newByteChannel() method is called. Larger the entry, higher the chances of the OutOfMemoryError.

A reproducer is attached which can be run as:

"java -Xmx256M TestZipFS.java"

 
Comments
Once this is fixed, the following short-circuit in `test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfFileTest.java` needs to be removed: https://github.com/openjdk/jdk/blob/3e5094ed12dbfad7587b85ae2168565682c1f1db/test/jdk/java/net/httpclient/HttpRequestBodyPublishers/OfFileTest.java#L161-L165
23-09-2025