Grizzly 内存管理
內(nèi)存管理概述
Grizzly 2.0引入了一個(gè)新的子系統(tǒng),以改善運(yùn)行時(shí)的內(nèi)存管理。該子系統(tǒng)由三個(gè)主要工件組成:
- 緩沖區(qū) Buffers
- 線程本地內(nèi)存池 Thread local memory pools
- MemoryManager, 作為工廠按序使用緩沖區(qū)和線程本地池
其主要目的是加快內(nèi)存分配,并在可能的情況下提供內(nèi)存重用。
以下各節(jié)將詳細(xì)描述這些概念。
MemoryManager
MemoryManager 是分配和取消已經(jīng)分配的 Buffer instance的主要接口:
package org.glassfish.grizzly.memory;import org.glassfish.grizzly.Buffer; import org.glassfish.grizzly.monitoring.MonitoringAware;/*** <tt>MemoryManager</tt>, responsible for allocating and releasing memory,* required during application runtime.* <tt>MemoryManager</tt> implementations work with Grizzly {@link Buffer}s.** @see Buffer** @author Alexey Stashok*/ public interface MemoryManager<E extends Buffer>extends MonitoringAware<MemoryProbe> {/*** <p>* The default {@link MemoryManager} implementation used by all created builder* instances.* </p>** <p>* The default may be changed by one of two methods:* <ul>* <li>* Setting the system property {@value MemoryManagerInitializer#DMM_PROP_NAME}* with the fully qualified name of the class that implements the* MemoryManager interface. Note that this class must be public and* have a public no-arg constructor.* </li>* <li>* Setting the system property {@value DefaultMemoryManagerFactory#DMMF_PROP_NAME}* with the fully qualified name of the class that implements the* {@link org.glassfish.grizzly.memory.DefaultMemoryManagerFactory} interface.* Note that this class must be public and have a public no-arg* constructor.* </li>* </ul>** </p>*/MemoryManager DEFAULT_MEMORY_MANAGER =MemoryManagerInitializer.initManager();/*** Allocated {@link Buffer} of the required size.** @param size {@link Buffer} size to be allocated.* @return allocated {@link Buffer}.*/E allocate(int size);/*** Allocated {@link Buffer} at least of the provided size.* This could be useful for usecases 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}.*/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.** @param oldBuffer old {@link Buffer} to be reallocated.* @param newSize new {@link Buffer} required size.* @return reallocated {@link Buffer}.*/E reallocate(E oldBuffer, int newSize);/*** 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 <tt>MemoryManager</tt> implementation.** @param buffer {@link Buffer} to be released.*/void release(E buffer);/*** Return <tt>true</tt> 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 {@link java.nio.ByteBuffer}, or <tt>false</tt> otherwise.* * @param size* @return */boolean willAllocateDirect(int size); }通常只有一個(gè)MemoryManager服務(wù)于Grizzly運(yùn)行時(shí)中定義的所有傳輸。可以通過(guò)引用MemoryManager接口的靜態(tài)成員來(lái)獲得此MemoryManager:
MemoryManager DEFAULT_MEMORY_MANAGER = MemoryManagerInitializer.initManager();然而,可以通過(guò)定義系統(tǒng)屬性org.glassfish.grizzly.DEFAULT_MEMORY_MANAGER來(lái)定義自定義MemoryManager實(shí)現(xiàn),作為默認(rèn)的MemoryManager,
該系統(tǒng)屬性引用要使用的MemoryManager實(shí)現(xiàn)的完全限定的類名。請(qǐng)注意,此實(shí)現(xiàn)必須具有公共的無(wú)參數(shù)構(gòu)造函數(shù),以便運(yùn)行時(shí)正確設(shè)置新的默認(rèn)值。
Grizzly 2.3包含兩個(gè)MemoryManager實(shí)現(xiàn):HeapMemoryManager和ByteBufferManager。默認(rèn)情況下,Grizzly運(yùn)行時(shí)將使用HeapMemoryManager,
但是,如果Grizzly應(yīng)用程序需要直接ByteBuffer訪問,則可以使用ByteBufferManager。
ByteBufferManager
該ByteBufferManager實(shí)施VENDS grizzly 緩沖實(shí)例那套JDK的ByteBuffer實(shí)例。如果Grizzly應(yīng)用程序需要直接使用ByteBuffer,則使用此MemoryManager。
應(yīng)該注意的是,在進(jìn)行基準(zhǔn)測(cè)試期間,此 MemoryManager 通常在使用堆緩沖區(qū)時(shí)需要更多的開銷。因此,如果不需要直接讀取內(nèi)存,我們建議使用默認(rèn)的HeapMemoryManager。
HeapMemoryManager
HeapMemoryManager 是默認(rèn)的 MemoryManager。代替包裝 ByteBuffer 實(shí)例,此 MemoryManager 將分配直接包裝字節(jié)數(shù)組的Buffer實(shí)例。
此 MemoryManager 為 trimming 或 splitting 之類的操作提供更好的性能特征。
ThreadLocal Memory Pools
ThreadLocal 內(nèi)存池提供了無(wú)需任何同步成本即可分配內(nèi)存的功能。無(wú)論是 ByteBufferManager 和 HeapMemoryManager 使用這些池。請(qǐng)注意,
不需要自定義 MemoryManager 使用此類池,但是,如果該 MemoryManager 實(shí)現(xiàn) ThreadLocalPoolProvider 接口,則必須提供 ThreadLocalPool 實(shí)現(xiàn)。
該 ThreadLocalPool 執(zhí)行將被創(chuàng)建并傳遞給每個(gè)通過(guò) Grizzly 管理維持的線程。
Memory Manager and ThreadLocal Memory Pools Working Together
以下提供了使用ThreadLocalPool向MemoryManager分配請(qǐng)求通常如何工作的流程圖:
緩沖區(qū) Buffers
Grizzly 2.3提供了一些緩沖區(qū),供開發(fā)人員在創(chuàng)建應(yīng)用程序時(shí)使用。這些Buffer實(shí)現(xiàn)一些功能,這些功能是使用JDK的ByteBuffer時(shí)沒有的。
Buffer
該緩沖器本質(zhì)上是模擬到JDK的字節(jié)緩沖區(qū)。它提供了以下相同的方法集:
- 向 Buffer 推入/拉出 (pushing/pulling) 數(shù)據(jù)。
- 用于訪問或操縱緩沖區(qū)的位置(position),限制(limit)和容量(capacity)的方法。
除了為ByteBuffer提供熟悉的語(yǔ)義外,還提供以下功能:
- 分割,修剪和縮小。 splitting, trimming and shrinking
- 在當(dāng)前緩沖區(qū)之前添加另一個(gè)緩沖區(qū)的內(nèi)容。 Prepending another Buffer’s content to the current Buffer.
- 將緩沖區(qū)轉(zhuǎn)換為ByteBuffer或ByteBuffer []。 Converting the Buffer to a ByteBuffer or ByteBuffer[].
- 將緩沖區(qū)內(nèi)容轉(zhuǎn)換為字符串。 Converting Buffer content to a String.
請(qǐng)參閱javadocs以獲取有關(guān)Buffer的更多詳細(xì)信息。
CompositeBuffer
該CompositeBuffer是另一個(gè)緩沖區(qū)實(shí)現(xiàn)它允許附加的緩沖實(shí)例。所述CompositeBuffer維護(hù)虛擬位置,限制和基于容量緩沖器已所附和可以被視為簡(jiǎn)單的緩沖液的實(shí)例。
有關(guān)CompositeBuffer的更多詳細(xì)信息,請(qǐng)參見javadocs 。
總結(jié)
以上是生活随笔為你收集整理的Grizzly 内存管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 飞凌嵌入式 OKA40i-C 开发板调试
- 下一篇: 工作笔记:飞凌嵌入式试用记录