powered by Jive Software

OutOfMemoryError: PacketCollector issues?

Hey everyone.

Here’'s what I keep getting:

Exception in thread “Smack Packet Writer” java.lang.OutOfMemoryError: Java heap space

And my code was as follows, surrounded by a while(true) loop:

this.packetCollector = this.xmppConnection.createPacketCollector(new PacketTypeFilter(IQ.class));

Packet receivedPacket = this.packetCollector.nextResult(timeoutMsec);


After doing some searching through the forums, I found out that I was forgetting to cancel() my PacketCollector’'s. So my code became:

this.packetCollector = this.xmppConnection.createPacketCollector(new PacketTypeFilter(IQ.class));

Packet receivedPacket = this.packetCollector.nextResult(timeoutMsec);



However, I still get the OutOfMemoryError’'s. Anyone have any ideas? Thanks!

you said it was surrounded by a while loop? you don’'t need to create a new PacketCollector everytime, once should do it.

Thanks for the response, AWenckus.:slight_smile:

OK, so my while(true) now looks like:

Packet receivedPacket = this.packetCollector.nextResult(timeoutMsec);[/code]

The packet collector is a class-level and I instatiate it before the while loop.

Although it takes a few hours to reach OutOfMemoryError status, I know it will happen because that’‘s what I had in the first place. Looking at the Window Task Manager, I can see the process grow by a few dozen KB every two seconds or so (that’‘s my timeout for nextResult()). I don’‘t know if that’‘s an accurate measurement, but rest assured, I will get an OutOfMemoryError in the next few hours. I’'ll run with a small heap size to expedite the error.


Packet receivedPacket;


outside of the while loop


receivedPacket = this.packetCollector.nextResule(timeoutMsec);


inside. I don’'t think that is your problem but it might help. Are you doing anything else in the loop? Are you putting those packets into a collection or anything? What are you doing with receivedPacket?

Out of memory errors are deceptive in that the stack traces generally don’‘t point to the source of the problem. What’'s happening is that objects are being kept in memory , there are references to them and they cannot be garbage collected, and the jvm is running out of memory. If I can get a little more contextual information, I could perhaps figure out where your program is failing.

I just did what you suggested, but it had no effect.

I was hoping not to have to go into the details of what I’‘m doing, but I guess there’'s no way around it:

There are two parts, each running in its own thread using the same JVM:

  1. data provider user, which accepts IQ GET packets and sends IQ RESULTs

  2. data distributor user, which sends IQ GET packets to the data provider user and posts the data from the IQ RESULTs to a MultiUserChat via a Message

The data distributor user is in a loop and sends an IQ GET with about 100 properties (set using setProperty()) every two seconds. The data provider user will get the IQ GET, and reply with an IQ RESULT containing about 100 properties with some values (mostly double’'s).

Using a memory profiler, I discovered that the Message objects are never being garbage-collected. A new one is created every two seconds, and each one has its own HashMap with about 100 properties with close to 100 double’'s.

So, running for just a few minutes will result in hundreds of Message’‘s that have not been GC’'ed.

I don’‘t think this is an issue with PacketCollector, so I’‘m thinking about re-posting this. Any more ideas, AWenckus? I appreciate all the help you’'ve given so far.