Issue Details (XML | Word | Printable)

Key: DIRMINA-78
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Trustin Lee
Reporter: Magnus Naeslund
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
MINA

ByteBuffer.wrap((byte[],?) The element of least suprise

Created: 23/Jul/05 02:14 AM   Updated: 26/May/09 12:45 AM
Return to search
Component/s: None
Affects Version/s: 0.7.0, 0.7.1, 0.7.2, 0.7.3
Fix Version/s: 0.7.4

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works mina-bytebuffer-wrap-example.diff 2005-07-23 02:17 AM Magnus Naeslund 3 kB
File Licensed for inclusion in ASF works mina-bytebuffer-wrap-fix-1.diff 2005-07-24 06:34 AM Magnus Naeslund 4 kB

Resolution Date: 17/Aug/05 12:02 PM


 Description  « Hide
*This is just an discussion, not a bug*

The problem:
When mina wraps (buffers or arrays) external data, it keeps push()ing the nio buffer to the stacks of buffers, but it can't reuse them when wrapping (since they either already exist or was created from nio wrap()).

This could be fixed in two ways:

1) When wrapping, re-use buffers as usual, con: extra copy
2) Have a flag that causes the underlying nio buffer to not be pooled after release

I would rather see minas bytebuffer to copy the data than cause an memory leak.
I've just debugged a project that writes byte arrays (old code, needs to write arrays), and found the memory leak that is (kind of) described in the mina ByteBuffer documentation. I ended up with a unbounded leak of nio HeapByteBuffers.

I know that this is discouraged in the documentation, but I don't like these kind of suprises that you get from using mina without carefully reading the documentation (note that the javadoc warning isn't at the method level).
It's like the close(true) that doesn't flush the buffers before closing the session (I can't understand why anyone wants it the way it is now).

I know copy it's slower, but if you're using wrap(), you'll need to do something like that anyways...
I implemented the copy for byte arrays as an example, because it is the least modification, I'll attach it to this issue.

So after this little rant, what do you think about this suggestion?

Regards,
Magnus Naeslund


Magnus


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Magnus Naeslund added a comment - 23/Jul/05 02:17 AM
Example on solving this, but maybe not the cleanest solution (best might be to not pool it at all)

Magnus Naeslund made changes - 23/Jul/05 02:17 AM
Field Original Value New Value
Attachment mina-bytebuffer-wrap-example.diff [ 12311361 ]
Magnus Naeslund added a comment - 23/Jul/05 02:23 AM
Maybe i should mention that the patch is totally untested :)

Trustin Lee added a comment - 23/Jul/05 09:28 PM
Hi, Magnus.

I agree with current documentation can just let user misuse wrap() methods. But users will still require legacy wrap() methods for performance reason. What do you think about renaming existing wrap methods to 'fastwrap' or something else, and add new wrap() methods that is safer. Any better ideas?


Magnus Naeslund added a comment - 24/Jul/05 04:05 AM
Well, the more performant way of doing this would be to have a flag in DefaultByteBuffer that says wether to push the NIO buffer into the stack pool on release().

So when we're using wrap it would still pool the mina bytebuffer container as we do now, but release() would not shove the NIO buffer back into the pool.

Magnus Naeslund added a comment - 24/Jul/05 06:34 AM
I whipped up a patch for showing how we could still have performance with wrapped but no memory leaks.
Totally untested, but it compiles :)

Magnus Naeslund made changes - 24/Jul/05 06:34 AM
Attachment mina-bytebuffer-wrap-fix-1.diff [ 12311371 ]
Trustin Lee added a comment - 24/Jul/05 10:25 AM
It is a good idea to specify to pool or not to pool existing NIO buffers and byte arrays internally. I'll try to apply this idea soon, and get back here when done.

Trustin Lee made changes - 24/Jul/05 10:26 AM
Fix Version/s 0.7.4 [ 12310182 ]
Affects Version/s 0.7.3 [ 12310110 ]
Affects Version/s 0.7.1 [ 11171 ]
Affects Version/s 0.7.2 [ 11180 ]
Affects Version/s 0.7 [ 11067 ]
Repository Revision Date User Message
ASF #233110 Wed Aug 17 03:00:08 UTC 2005 trustin Fix for DIRMINA-78 - ByteBuffer.wrap((byte[],?) The element of least suprise:
* Added 'pooled' property to ByteBuffer
* Revised JavaDocs to guide users to use setPooled() method instead of additional acquire()
* Added a test case related with 'pooled' property.
Files Changed
MODIFY /directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
MODIFY /directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
MODIFY /directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBufferProxy.java
MODIFY /directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java
MODIFY /directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java
MODIFY /directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java

Trustin Lee added a comment - 17/Aug/05 12:02 PM
I added a property called 'pooled' to MINA ByteBuffer. This property is 'true' by default except for the case you created the wrapped buffer. In case of the wrapped ones, it is 'false' by default so that they are not returned to the buffer pool. Of course you can change this bahavior by calling 'setPooled' method.

I mark this issue as 'resolved' because it resolves all issues addressed here by users who have a concern on it.

Trustin Lee made changes - 17/Aug/05 12:02 PM
Status Open [ 1 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Emmanuel Lecharny made changes - 26/May/09 12:45 AM
Status Resolved [ 5 ] Closed [ 6 ]