我在java Socket对象通信中遇到内存泄漏问题。java socket对象内存泄漏
这是我的发送主题。
// create a new thread to send the packet
@Override
public synchronized void run() {
if(!genericSocket.isConnected()){
if(logger.isEnabled())
logger.logMessage(PFLogging.LEVEL_WARN, "Socket is close");
return;
}
int retry = 0;
boolean packetSent = false;
synchronized (objWriter) {
while ((retry < RETRY) && (!packetSent) && (genericSocket.isConnected())) {
try {
objWriter.writeObject(bean);
objWriter.flush();
// Try until the cache is reset and the memory is free
/*
boolean resetDone = false;
while(!resetDone) {
try {
objWriter.reset();
resetDone = true;
} catch (IOException r) {
Thread.sleep(1);
}
}
*/
// No error and packet sent
continuousError = 0;
packetSent = true;
} catch (Exception e) {
continuousError++;
if(logger.isEnabled())
logger.logMessage(PFLogging.LEVEL_ERROR, "Continuous Error [" + continuousError + "] sending message [" + e.getMessage() + "," + e.getCause() + "]");
// control the number of continuous errors
if(continuousError >= CONTINUOUS_ERROR) {
if(logger.isEnabled())
logger.logMessage(PFLogging.LEVEL_WARN, "I close the socket");
genericSocket.disconnect();
}
// next time is the time!
retry++;
}
}
}
}
缓存,当我发送有关我每包毫秒增长和增长!
如果我添加注释的部分缓存干净,但当我需要发送异步长消息(约3000字符)时,我看到其他消息丢失!
还有另一种清除缓存而不重置的方法吗?
我现在但如果我重置缓冲区有很多情况下我丢失数据包(此套接字用于异步和同步消息) –
@MatteoGatto reset()不会导致数据包丢失。 – EJP
@MatteoGatto可能会导致您多次发送相同的对象。流的常见错误是通过两个不同的Streams(例如ObjectOutputStream和PrintWriter)或两个ObjectOutputStreams来写或读。这将导致数据损坏。 –