robbat2: (Default)
robbat2 ([personal profile] robbat2) wrote2005-03-29 09:20 pm

(Un)Fun with Java - Memory leaks by design... = bad design.

ObjectOutputStream oos = new ObjectOutputStream(...);
...
write lots of objects to the ObjectOutputStream.
...

get OutOfMemory exception :-(.

ObjectOutputStream keeps a record of every object it has seen, and thus prevents the object from being garbage-collected, even when you ARE done with it.
ext_85396: (Default)

[identity profile] unixronin.livejournal.com 2005-03-30 07:23 am (UTC)(link)
Eeeewww. That's ugly.

[identity profile] deskitty.livejournal.com 2005-03-30 08:34 am (UTC)(link)
Yet one more reason why I use C++, and generally refuse to write in Java if I can help it ...

Yes, C++ is the misfit of the family, with two heads, three arms and a hunchback, but at least it doesn't talk your ear off and eat all the dessert at family gatherings. ;P

[identity profile] robbat2.livejournal.com 2005-03-30 08:52 am (UTC)(link)
I've got the protocol docs for this app, but if I wanted to write it in anything else, I'd have to sort out a compiler for the machine it has to run on (they only have java :-().

[identity profile] musicdieu.livejournal.com 2005-03-30 08:08 pm (UTC)(link)
Assuming:
- you can't change ObjectOutputStream
- you can't use another language

Can't you periodically delete the ObjectOutputStream object (just create a new one to replace it, call gc() if memory serves me correctly), replace it with a new one?

[identity profile] robbat2.livejournal.com 2005-03-30 09:51 pm (UTC)(link)
your assumptions are correct.

I've tried what you suggest, but the output that I get from it doesn't work with ObjectInputStream.

It expects a complete stream, and when you close the OutputStream, it writes a termination marker (and every after that gets ignored by the ObjectInputStream).

The hack I've taken up for the moment is to simply abuse the reset() function of ObjectInputStream after every N calls to write something. This really slows things down, but it's not production code, and speed isn't an issue for me.

[identity profile] amethest.livejournal.com 2005-03-31 09:50 am (UTC)(link)
Run 10: Turns Robbat2 into a very small shell script
Run 20: Makes multiple copies of very small shell script
Run 30: Scatters copies over Amethest's virtual bed
Run 40: Rolls around and tickles and cuddles very small shell script copies

/me sigh.

POST SOMETHING REAL!