小编Cur*_*rge的帖子

强制显式删除Java对象

我正在研究处理大量非常密集流量的Java服务器.服务器接受来自客户端的数据包(通常是几兆字节)并将它们转发给其他客户端.服务器从不显式存储任何传入/传出数据包.然而,服务器不断遇到OutOfMemoryException异常.

我添加System.gc()到服务器的消息传递组件中,希望释放内存.另外,我将JVM的堆大小设置为千兆字节.我仍然得到了许多例外.

所以我的问题是:如何确保兆字节消息无限期排队(尽管不需要)?有没有办法让我在这些对象上调用"delete"来保证它们不使用我的堆空间?

        try
        {
           while (true)
            {
               int r = generator.nextInt(100);//generate a random number between 0 and 100
                Object o =readFromServer.readObject();
                sum++;
                // if the random number is larger than the drop rate, send the object to client, else
                //it will be dropped
                if (r > dropRate)
                {
                    writeToClient.writeObject(o);
                    writeToClient.flush();
                    numOfSend++;
                    System.out.printf("No. %d send\n",sum);
                }//if

            }//while
        }//try
Run Code Online (Sandbox Code Playgroud)

java garbage-collection dealloc

14
推荐指数
3
解决办法
2万
查看次数

标签 统计

dealloc ×1

garbage-collection ×1

java ×1