我正在开发一个处理大量高密度流量的Java服务器。该服务器接受来自客户端的数据包(通常为数兆字节),并将它们转发给其他客户端。服务器从不显式存储任何传入/传出的数据包。然而,服务器总是遇到OutOfMemoryException
异常。
我在服务器的消息传递组件中添加了System.gc()
,希望可以释放内存。此外,我将JVM的堆大小设置为1GB。但我仍然会收到同样数量的异常。
因此我的问题是:如何确保这些数兆字节的消息不会无限期地排队(尽管它们没有被使用)?我能否以某种方式调用"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
System.gc()
调用,它根本不起作用。 - Esko