Search code examples
javasocketstcpserversocket

TCP client socket filling up heap


Using java socket library and what I see is that when the server accepts the tcp connection but not read data from the buffer and in the meantime our client keep pumping heavy data, client side heap size grows to max available and crushes the client application. Is there anyway I can limit the socket buffer size or whatever using that heap and if it is violated, connection is killed? This way client can detect client detect the connection is gone and

Client Side;

  this.socket = new Socket(server, port);
  this.socket.setKeepAlive(true);
  this.outwriter = new OutputStreamWriter(this.socket.getOutputStream());
  this.outwriter.write(o.toString()); 

Server Side;

conn = serverSocket.accept();

Solution

  • This is not cause by the socket. You have a memory leak elsewhere in your application. The behaviour you describe will eventually block in the write() method and will not consume any heap memory whatsoever.