xuquan213
午夜前醒来丶
2019-10-19 11:36

Netty作为socket长链接,在windows部署占用38M左右内存,在linux部署占用1.5G左右内存

不知各位有遇到过这样问题的吗?

图片说明
这是在linux部署直接就这样了
关于启动类我给大家粘一下代码

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //服务端要建立两个group,一个负责接收客户端的连接,一个负责处理数据传输
                //连接处理group
                EventLoopGroup boss = new NioEventLoopGroup();
                //事件处理group
                EventLoopGroup worker = new NioEventLoopGroup();
                ServerBootstrap bootstrap = new ServerBootstrap();
                // 绑定处理group
                bootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
                        //保持连接数
                        .option(ChannelOption.SO_BACKLOG, 100000)
                        //有数据立即发送
                        .option(ChannelOption.TCP_NODELAY, true)
                        //保持连接
                        .childOption(ChannelOption.SO_KEEPALIVE, true)
                        //处理新连接
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel sc) throws Exception {
                                // 增加任务处理
                                ChannelPipeline p = sc.pipeline();
                                p.addLast(new IdleStateHandler(new Integer(new ReadProperties().getUrlValue("read.time.seconds")),0,0));
                                p.addLast(new ServerIdleStateTrigger());
                                p.addLast(new Decoder());
                                p.addLast(new Encoder());
                                p.addLast(new ServerHandler());

                            }
                        }).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

                //绑定端口,同步等待成功
                ChannelFuture future;
                try {
                    future = bootstrap.bind(serverPort).sync();
                    if (future.isSuccess()) {
                        serverSocketChannel = (ServerSocketChannel) future.channel();
//                        System.out.println("服务端开启成功");
                        // 启动定时删除文件的线程
                        new Thread(new DeleteFile()).start();
                    } else {
//                        System.out.println("服务端开启失败");
                    }

                    //等待服务监听端口关闭,就是由于这里会将线程阻塞,导致无法发送信息,所以我这里开了线程
                    future.channel().closeFuture().sync();
                } catch (Exception e) {
                    // e.printStackTrace();
                }
                finally {
                    //优雅地退出,释放线程池资源
                    boss.shutdownGracefully();
                    worker.shutdownGracefully();
                }
            }
        });

handler类继承SimpleChannelInboundHandler

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答