1
votes

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();
2

2 Answers

2
votes

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.

0
votes

You can use socket.setSendBufferSize().

Though this would help in testing your theory,as mentioned by @EJP, the heap leak will not be due to send buffer getting filled up. In which case the write operation will return error.