I have a problem with my Java service running out of memory. The code is essentially:
welcomeSocket = new ServerSocket(triggerPort);
welcomeSocket.setSoTimeout(3000);
while(running) {
try { connectionSocket = welcomeSocket.accept();
if (connectionSocket != null) {
ReaderThread t = new ReaderThread(connectionSocket.getInputStream(), connectionSocket.getOutputStream());
t.start();
}
} catch(SocketTimeoutException timeOutEx) {}
}
Using some memory leak analysis software the problem seems to be that welcomeSocket.accept() creates a number of objects that are never garbage collected. I can't find a way of getting rid of these objects while still allowing the new thread (which uses the same port) to work.
If I incremented a variable in the loop and exited the loop when it reached a certain number would this stop the service and allow the automatic restart to kick in, clearing the memory in the process? Any other suggestions for resolving the problem?
Thanks
Sue
FOLLOW US
Get the latest news in Application Packaging