Grizzly-Core Framework

来源:网络收集 时间:2025-06-28 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xuecool-com或QQ:370150219 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

一、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 extendsJmxMonitoringAware { /**

* 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线程去处理

百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库Grizzly-Core Framework在线全文阅读。

Grizzly-Core Framework.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.70edu.com/wenku/583758.html(转载请注明文章来源)
Copyright © 2020-2025 70教育网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:370150219 邮箱:370150219@qq.com
苏ICP备16052595号-17
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:7 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219