Trouble with ObjectInputStream.readObject() and memory

Hello…
I am using serialization to send stuff from a client to a server… here is the server loop code:

try{
    while(runMe){

       Object obj = myStreamReader.readObject();
       incomingMessage=(NetworkMessage)obj;
     
       parseInput(); //Parses the client request and sends an answer
       incomingMessage=null;

   }
}catch(Exception e){System.out.println("Connection thread: error "+e+" handling client for "+mySocket);}

I don’t know what I m doing wrong… but I get a memory leak, the heap keeps growing and growing, until I get the outofmemory exception. I tried commenting out the parseInput() method, in case I m keeping some open references during the handling of the messages… nothing…
Is there anything I need to do to clear the ObjectInputStream 's own references to the retrieved objects? The problem seems to lie there, but I wasn’t able to find any info about it.
Here is the reference tree:

http://mauryr.bounceme.net/images/references.gif

I seem to understand from this the only reference is from the ObjectInputStream itself.
Thanks everybody for your help.

I the code you gave us, incomingMessage is undefined so by its nature its invomplete.

The answer to your questio though is to fire up a profiler. See what is being leaked and where.

If it stil lisnt obvious to you after that, give us that in formation and the code that handles whatever the obejct in question is and maybe we can help.

When you are writing the object, use a loop like,

ObjectOutpuStream out = …

while(…)
{
out.writeObject(…);
out.reset();
}

The object streams remember every object written to them unless you reset them.

private ObjectOutputStream myStreamWriter=null;
private ObjectInputStream myStreamReader=null;
private ConnectionServer connectionServer=null;
private int sentMsgCounter;

public void init(){
try{
this.myStreamWriter= new ObjectOutputStream (new BufferedOutputStream(mySocket.getOutputStream()));
this.myStreamWriter.flush();
this.myStreamReader = new ObjectInputStream(new BufferedInputStream(mySocket.getInputStream()));
}catch(Exception e){System.out.println("Error “+e+” handling client for "+mySocket);}
}
public void sendData(NetworkMessage message){
try{
myStreamWriter.writeObject(message);
myStreamWriter.flush();
if(++sentMsgCounter>= RESET_FREQUENCY ){
sentMsgCounter = 0;
// Failing to reset the object output stream every now and
// then creates a serious memory leak.
myStreamWriter.reset();
}
}catch(IOException e){System.out.println(e);};
return;
}

Init is the initialization function (uh uh ~ ) and sendData is the writer one, everything looks fine to me… but obviously isn’t, for some reason.
I think the problem is with the reader, not with the writer. incomingMessage is defined as a member of the class, and it’s of type NetworkMessage. As you can guess, the leak is on instances of NetworkMessage… which the profiler says are instanced by ObjectInputStream.readObject()

After about 2 hrs running, JProfiler displays: 100.0% - 6.469 kB - 404.314 alloc. java.io.ObjectInputStream.readObject

Everything hints the incriminated references are inside the ObjectInputStream itself (look at the picture in the original post, it’s a view of the references to NetworkMessage per JProfiler) .

Update
I just tried calling ObjectOutputStream.reset( ) every now in the reader function, but it doesn’t help either.

Ok, I think I got it fixed… I found out you need to call .reset( ) not long before I posted this, so I was only using it on the server, it needs to be done on both client and server to work properly. I thought “let’s fix server first, then I can do client”… what a bad idea =)

Thanks all for your help.