I am working on a small java network program for instant messages using sockets. In the application the messages are read using the DataOutputStream's
readUTF()
method, which return a String object
. So every time a new message is arrived or sent a new String object is created in the memory, which in turn consumes memory. After a long time of communication there are several String objects in the memory.
So is there any way I can avoid this or I should try some other way of recieving messages. I am very new to Java network programming and its concepts. Thanks. I have a very bad coding style still I will try to make the question clear.
String recievedMessage = dataInputStream.readUTF();
String messageType = recievedMessage.substring(0, recievedMessage.indexOf("##"));
String message = recievedMessage.substring(recievedMessage.indexOf("##") + 2, recievedMessage.indexOf("$$"));
After this the message object is inserted in a JTextPane
using a StyledDocument
.