What is best to stock incoming messages : custom buffer pool or "new byte[n]" ?

Hello,

When our servers receives some variable sized messages that need to be stocked in a queue, what is the best practice to allocate some space where to put those messages :

  • Simply to use the “new byte[n]” and let the JVM handle the memory,
  • Or handle our own stack of buffers organized in a special-home-made way ? (if this is the best for performances, please explain how to handle them, please).

Karma.

if by buffers, you mean direct-byte-buffers, it’s better to allocate a big one, and use duplicate it with smaller subranges. On Windows the minimum allocated size is 4k*, so a lot of tiny buffers would consume much more RAM than one split into pieces.

* got that from these forums - haven’t checked it

I was thinking more about performances in terms of speed rather that memory usage, even if I still care about memory usage.

Note: The JVM is probably not using the allocation from Windows, so forget the 4k thing.

Hmm, I would let java handle the memory management as long as you don’t have a critical performance problem. Maybe using Jakarta Collections is an option.

Once you have a problem, profile the application before trying an homemade alternative.

I can’t do that, my program is a network library.

What library is that? I’m amazed at how many people write their own networking API…it seems so counter-productive to me. :o

I wish we could all just collaborate on one API and make it the best one with all the features anyone might need instead of having 50 mediocre ones.

-Matt Hicks

I guess http://jnag.dev.java.net ;D

Ya know…after a while those little signature bars just disapear from my vision…or maybe I just need glasses. :-p

Thanks for pointing that out Herkules. :-p

Traditionally this is done with ring buffers.

The nice things about a ring buffer are:
(a)Its memory usage is fixed and
(b)If it runs out of memory it starts by eliminate the oldest data.

There is no prebuilt Ring Buffer type in nio and whenever Ive tried to turn a Buffer into a ring buffer its seemed inordinately complex, so today id proably do it around an array though i WOULD like to solve the general case of wrapping ring logic around any Buffer subclass…

Can you describe a little more how your ring buffer is working ?

There is something to take into account, it is that in my case (JNAG) the messages are not released in the same order that they are allocated since it is done by a pool of threads.

Karma

hrm.

If its not a FIFO queue then a ring buffer wont work.

I saw that mina (the project from Apache) is using a home-made pool of buffers implemented in the org.apache.mina.common.ByteBuffer.
I will take a look at it asap, it seems to be made for the case of JNAG (i.e. multi thread processing the messages ;D).

Hey ! You spoke about JNAG when you proposed your project and now you don’t know what it is ? ???
You also forgot about me ? I gave my “yes” for your project when you proposed it, putting the emphasis on more choice for the community.

[quote=“sunsett,post:6,topic:26196”]
By posting some questions here in the forum about some precise details of the implementations of network libraries, I am also hoping to contribute to help the other libraries to improve.

By the way, the 50 guys that try hard to make their library say thank you to you for your moral support. Just a stupid question : What makes you believe that your library is better than the other ones ? Is it because it is a library that you wrote ? And to enlight your curiosity, I started my library because I don’t like the existing ones and I continue it because I still don’t like the existing ones. That’s also probably the same for the 50 others that you are speaking about.

Karma

Yes! And … I logged a bug against NIO for this over a year ago :(. It’s ridiculous trying to do serious buffer-based I/O without ring buffers :(.

What’s his ID number ? I want to take a look at it.