一、Core Framework
1.1Memory Management
Memory Management Overview
Grizzly 2.0 引入了一个新的子系统来改进运行时的内存管理。该子系统包含3部分主要内容: ? Buffers
? ThreadLocal memory pools
? MemoryManager as a factory of sorts using the buffers and thread local pools
其主要目标是加速内存分配,以及如果可能,提供内存复用。接下来将详细讲述这些概念。
MemoryManager
MemoryManager是分配和解除分配 Buffer 实例的主要接口:
publicinterfaceMemoryManager
* Allocated {@link Buffer} of the required size. *
* @param size {@link Buffer} size to be allocated. * @return allocated {@link Buffer}. */
public E allocate(int size); /**
* Allocated {@link Buffer} at least of the provided size.
* This could be useful for use cases like Socket.read(...), where * we're not sure how many bytes are available, but want to read as * much as possible. *
* @param size the min {@link Buffer} size to be allocated. * @return allocated {@link Buffer}. */
public E allocateAtLeast(int size); /**
* Reallocate {@link Buffer} to a required size.
* Implementation may choose the way, how reallocation could be done, either
* by allocating new {@link Buffer} of required size and copying old * {@link Buffer} content there, or perform more complex logic related to
* memory pooling etc. *
* @paramoldBuffer old {@link Buffer} to be reallocated. * @paramnewSize new {@link Buffer} required size. * @return reallocated {@link Buffer}. */
public E reallocate(E oldBuffer, intnewSize); /**
* Release {@link Buffer}.
* Implementation may ignore releasing and let JVM Garbage collector to take
* care about the {@link Buffer}, or return {@link Buffer} to pool, in case
* of more complex MemoryManager implementation. *
* @param buffer {@link Buffer} to be released. */
publicvoid release(E b;uffer);
/**
* Return true if next {@link #allocate(int)} or {@link #allocateAtLeast(int)} call,
* made in the current thread for the given memory size, going to return a {@link Buffer} based
* on direct {@linkjava.nio.ByteBuffer}, or false otherwise. *
* @param size * @return */
publicbooleanwillAllocateDirect(int size); }
在Grizzly运行时定义了一个典型的MemoryManager,用来服务于所有传输。通过引用MemoryManager接口的静态成员变量MemoryManager可以得到。
MemoryManager.DEFAULT_MEMORY_MANAGER
反之,也可以自定义MemoryManager的实现来作为默认的MemoryManager,通过指定系统属性org.glassfish.grizzly.DEFAULT_MEMORY_MANAGER为自定义MemoryManager实现。 注意:该实现必须包含一个public的无参构造函数,以便runtime to properly set the new default。 Grizzly 2.3 自带两个MemoryManager实现:HeapMemoryManager和ByteBufferManager。 默认情况下,Grizzly运行时使用HeapMemoryManager,如果Grizzly应用需要直接访问ByteBuffer,就可以使用ByteBufferManager。
ByteBufferManager
The ByteBufferManager implementation vends Grizzly Buffer instances that wrap JDK ByteBuffer instances. This is the MemoryManager to use if the Grizzly application requires direct ByteBuffer usage.
It should be noted that this MemoryManager, during our benchmarking, showed to have a little more overhead when using typical heap buffers. As such, if direct memory isn’t needed, we recommend that the default HeapMemoryManager be used.(如果不需要直接内存,建议使用HeapMemoryManager)
HeapMemoryManager
The HeapMemoryManager is the default MemoryManager. Instead of wrapping ByteBuffer instances, this MemoryManager will allocate Buffer instances that wrap byte arrays directly. This
MemoryManager offers better performance characteristics for operations such as trimming or splitting.
ThreadLocal Memory Pools
ThreadLocal内存池在分配内存时,没有任何同步成本。ByteBufferManager和HeapMemoryManager都是使用的这个池。 注意:自定义的MemoryManager可以不用这个池,但是如果说MemoryManager实现了ThreadLocalPoolProvider接口,就必须提供ThreadLocalPool的实现。ThreadLocalPool的实现会被创建和传递给Grizzly管理的每个线程。
Memory Manager and ThreadLocal Memory Pools Working Together
下图是带有TreadLocalPool的MemoryManager管理内存分配的工作图:
Buffers
Grizzly 2.3 提供了多个Buffers供开发者创建应用时使用。这些Buffer实现提供了很多
JDK ByteBuffer不具有的特性。
Buffer
The Buffer is essentially the analogue to the JDK’s ByteBuffer. It offers the same set of methods for:
? Pushing/pulling data to/from the Buffer.
?
Methods for accessing or manipulating the Buffer’s position, limit, and capacity.
In addition to offering familiar semantics to ByteBuffer, the following features are available: ? Splitting, trimming, and shrinking.
? ? ?
Prepending another Buffer’s content to the current Buffer. Converting the Buffer to a ByteBuffer or ByteBuffer[]. Converting Buffer content to a String.
Please see the javadocs for further details on Buffer.
CompositeBuffer
The CompositeBuffer is another Buffer implementation which allows appending
of Buffer instances. The CompositeBuffer maintains a virtual position, limit, and capacity based on the Buffers that have been appended and can be treated as a simple Buffer instance. Please see the javadocs for further details on CompositeBuffer.
1.2 I/O Strategies
IOStrategies
使用NIO时,有个很自然的问题,发生在NIO channel上的特定NIO event是如何处理的。通常有两种选项:在当前线程(selector)中处理NIO事件,或者传到worker线程去处理。
1.Worker-thread IOStrategy.
最有用的IO策略,Selector线程把NIO事件委托给worker线程去处理