我在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來寫或讀。這將導致數據損壞。 –