Dan Lawesson a écrit :
Hi,
Until yesterday I was under the impression that heap buffers are the
way to go and direct buffers are more of a relic of at most historical
value. This was a belief that was at least partly supported by the
Apache MINA project documentation of the upcoming version 2.0.
Yesterday my view on heap buffers was completely reversed when I
witnessed a perfectly working legacy system (Java 1.4.2 something)
barf and die due to lack of direct memory even though we did not
explicitly use any direct heap buffers at all. Post mortem heap dumps
show that there had been a creation of 100 direct buffers per second
and none of them were cleaned since heap memory was fine and no full
GC sweep was triggered. The heap contained a chain of 200k cleaners
after 30 minutes of uptime. The reason I have not witnessed this
before is probably that usually these systems also use a lot of heap
memory and therefore trigger full GC which triggers the cleaners.
After a while we realized that in case you use heap buffers, you get a
temporary direct buffer. Clearly, at least in this old JVM these
temporary direct buffers are not even pooled (that was the WTF of the
day!).
This lead me to DIRMINA-391
http://issues.apache.org/jira/browse/DIRMINA-391. Trustin Lee
advocates heap buffers, even though the issue presents a strong case
against them. Does this mean that this behavior of using non-pooled
temporary direct buffers is a thing of the past, i.e., more modern
JVMs are smarter than that? This is the only explanation I can find to
why MINA 2.0 uses heap buffers per default and also has dropped the
pooling feature of older MINA versions.
The reason I write this mail is that both
https://issues.apache.org/jira/browse/DIRMINA-576 and the linked Sun
issue seem to contradict this. They both make a strong case for always
using pooled direct buffers. What is the community consensus on heap
buffers?
Concensus, I don't know. My strong opinion is that direct buffers are to
be used when dealing with low level IO, and heap buffers must be used
for everything else.
The fact that you can kill your OS because you eat all of it's memory
seems to me a big no-no for DirectBuffer. If I use Heap buffers and if I
suck up all my JVM memory, too bad for me, my code and my salary, but at
least, it impacts only my application's JVM.
In your case, the Direct buffers are used to exchange data with a socket
in a IOread, done by the JVM : makes perfect sense. The question is why
aren't they cleaned ?
I have to dig back into those issues I guess...