Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should call ByteBuf.recycle() after read data? #44

Closed
azzahrah opened this issue Feb 2, 2021 · 3 comments
Closed

Should call ByteBuf.recycle() after read data? #44

azzahrah opened this issue Feb 2, 2021 · 3 comments

Comments

@azzahrah
Copy link

azzahrah commented Feb 2, 2021

Should we recycle after read incoming data

 public static ByteBufsDecoder<ByteBuf> customDecoder(int maxSize) {

        return bufs -> {

            if (bufs.remainingBytes() < 5) {
                return null;
            }

            int length = 2 + 2; //header tail
            if (bufs.peekByte(0) == 0x78 && bufs.peekByte(1) == 0x78) {
                length += 1 + bufs.peekByte(2);
            } else if (bufs.peekByte(0) == 0x79 && bufs.peekByte(1) == 0x79) {
                length += 2 + bufs.peekByte(2);
            }

            if (bufs.remainingBytes() < length) {
                return null;
            }
            //ByteBuf temp = ByteBufPool.allocate(length);
            //bufs.drainTo(temp);
            //return temp;
            return bufs.takeExactSize(length);
        };
    }

Main App

 public static void main(String[] args) throws Exception {
        Eventloop eventloop = Eventloop.create().withCurrentThread();
        SimpleServer server = SimpleServer.create(eventloop, new Consumer<AsyncTcpSocket>() {
            @Override
            public void accept(AsyncTcpSocket socket) {
                
                // readData(socket);
                BinaryChannelSupplier
                        .of(ChannelSupplier.ofSocket(socket))
                        .decodeStream(customDecoder(1024))
                        .streamTo(ChannelConsumer.ofConsumer((data) -> {
                            //read data....
                            //should i call data.recycle() after read data?

                            data.recycle(); // Is this right?
                        }));
            }
        });
        server.withListenPort(PORT);
        server.listen();

        System.out.println("Server is running");
        System.out.println("You can connect from telnet with command: telnet localhost 9922 or by running csp.TcpClientExample");

        eventloop.run();
    }

"To make everything consistent, ActiveJ relies on the concept of ‘ownership’ (like in Rust language) - after allocation, the components pass a byteBuf from one to another, until the last ‘owner’ recycles it to ByteBufPool."

after read data? should call ByteBuf.recycle()? how about when client disconnect, shoudl activej recycle all buf have been used?

@azzahrah azzahrah changed the title When call ByteBuf.recycle after read data? Should call ByteBuf.recycle() after read data? Feb 2, 2021
@eduard-vasinskyi
Copy link
Contributor

Hello, @azzahrah
Yes, you should recycle a ByteBuf once you have read data from it and not planning to use it anymore. This way you will ease Java’s garbage collection and overall memory pressure. If you do not recycle a ByteBuf, it will still be collected during GC (if there are no references to the ByteBuf).
ActiveJ recycles all of the internal ByteBufs once they are not needed (e.g. connection closes, component shutdowns).

@azzahrah
Copy link
Author

azzahrah commented Feb 3, 2021

@eduard-vasinskyi , thank for your answer this clear for me.... this like ReleaseReference counting in netty

@eduard-vasinskyi
Copy link
Contributor

@azzahrah, yes, ByteBuf counts its references and returns buf to the pool once it reaches zero.

@azzahrah azzahrah closed this as completed Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants