ByteBuf deals directly with the underlying IO
1. What are the memory categories
2. How to reduce multi-thread memory allocation competition
3. How to allocate memory of different sizes
Abstraction of memory and memory manager
Allocation strategies for different sizes and memory types
Memory reclamation
ByteBuf structure
readerIndex, indicating that the data to be read starts from the current pointer, and the space from 0 to readerIndex indicates that it is invalid
writerIndex, must be greater than readerIndex, indicating that the data to be written starts from the current pointer, and the space from readerIndex to writerIndex indicates that it can be read
capacity, must, the space from writerIndex to capacity indicates free to write
ByteBuf API
read, start reading from readerIndex
write Start writing from writerIndex onwards
set does not move any pointer, set directly at the current pointer
markReaderIndex save readerIndex
resetReaderIndex will restore the readerIndex so that reading the data will not move the pointer
markWriterIndex Same as above
resetWriterIndex Same as above
readableBytes() writerIndex - readerIndex
writeableBytes() capacity - writerIndex
ByteBuf classification
Pooled and UnPooled, when allocating a memory from an already allocated memory, there is a pre-allocation process, UnPooled is to apply for memory directly from the operating system
Unsafe and non-UnSafe, Unsafe can directly get the memory address, you can directly get ByteBuf in the JVM memory through the memory address and offset, non-unsafe can directly call jdk api to read and write, access data through arrays and subscripts. jdk automatically determines Unsafe or non-Unsafe
Heap and Direct, directly allocated on the heap, jc automatic recycling management, relying on an array, Direct calls jdk api allocation will not be managed by jvm memory recycling, relying on jdk underlying ByteBuffer.
ByteBufAllocator memory manager
AbstractByteBufAllocator skeleton implementation
UnPooledByteBufAllocator, directly allocate an array of capacity
newHeapBuffer new array on the heap comes out and save
newDirectBuffer relies on the underlying ByteBuffer of jdk to save the initial address and capacity.
PooledByteBufAllocator, first get the thread local cache, PoolThreadCache first creates PoolArena <byte []>, PoolArena <ByteBuf> these two memory pools, and allocates memory on the Arena on the thread. When creating a memory allocator, two types of arrays are created, PoolArena.HeapArena and PoolArena.directArena
newHeapBuffer
newDirectBuffer