RFR: 8316156: (ch) Channels.newOutputStream(ch).write(bigByteArray) allocates a lot of direct memory

Alan Bateman alanb at openjdk.org
Thu Sep 14 06:33:38 UTC 2023


On Thu, 14 Sep 2023 03:05:50 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

> In `ChannelInputStream` and `ChannelOutputStream`, process small chunks of bytes in sequence to avoid running afoul of the `MaxDirectMemorySize`.

src/java.base/share/classes/sun/nio/ch/ChannelInputStream.java line 49:

> 47:  */
> 48: class ChannelInputStream extends InputStream {
> 49:     static final int DEFAULT_BUFFER_SIZE = 8192;

I don't think COS should be using CIS.DEFAULT_BUFFER_SIZE, maybe it should be moved to Streams which is the factory class for the streams based on channels.

src/java.base/share/classes/sun/nio/ch/ChannelInputStream.java line 68:

> 66:      * If the channel is selectable then it must be configured blocking.
> 67:      */
> 68:     private int readFully(ByteBuffer bb) throws IOException {

InputStream.read methods are not "read fully", it will cause the read to hang, say where you read some bytes and block indefinitely to fill the user's buffer.

test/jdk/java/nio/channels/Channels/WriteFullyMemorySize.java line 54:

> 52:                 throw new RuntimeException("Arrays are not equal");
> 53:         } finally {
> 54:             Files.delete(target);

The test is for CIS.read/COS.write so I guess it should directly exercise those methods.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15733#discussion_r1325421210
PR Review Comment: https://git.openjdk.org/jdk/pull/15733#discussion_r1325422565
PR Review Comment: https://git.openjdk.org/jdk/pull/15733#discussion_r1325422995


More information about the nio-dev mailing list