How to garbage collect a direct buffer in Java

I suspect that somewhere your application has a reference to the ByteBuffer instance(s) and that is preventing it from being garbage collected. The buffer memory for a direct ByteBuffer is allocated outside of the normal heap (so that the GC doesn’t move it!!). However, the ByteBuffer API provides no method for explicitly disposing of / … Read more

Why the odd performance curve differential between ByteBuffer.allocate() and ByteBuffer.allocateDirect()

How ByteBuffer works and why Direct (Byte)Buffers are the only truly useful now. first I am a bit surprised it’s not common knowledge but bear it w/ me Direct byte buffers allocate an address outside the java heap. This is utmost importance: all OS (and native C) functions can utilize that address w/o locking the … Read more

Java InputStream to ByteBuffer

For me the best in this case is Apache commons-io to handle this and similar tasks. The IOUtils type has a static method to read an InputStream and return a byte[]. InputStream is; byte[] bytes = IOUtils.toByteArray(is); Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). UPDATE: as long … Read more

How to put the content of a ByteBuffer into an OutputStream?

Look at Channels.newChannel(OutputStream). It will give you a channel given an OutputStream. With the WritableByteChannel adapter you can provide the ByteBuffer which will write it to the OutputStream. public void writeBuffer(ByteBuffer buffer, OutputStream stream) { WritableByteChannel channel = Channels.newChannel(stream); channel.write(buffer); } This should do the trick!

Exception in thread “main” java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

After searching for a while and verified through switching the installed JDK between 8 and 11, I have found that there are some changes (new overridden methods) applied to several methods (e.g. flip(), clear() ) in ByteBuffer class. In Java 8, while calling flip() method of ByteBuffer class, since it has no implementation for this … Read more

Growing ByteBuffer

In order for asynchronous I/O to work, you must have continuous memory. In C you can attempt to re-alloc an array, but in Java you must allocate new memory. You could write to a ByteArrayOutputStream, and then convert it to a ByteBuffer at the time you are ready to send it. The downside is you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)