netty源码阅读之ByteBuf之ByteBuf分类

电玩女神 2022-05-14 23:27 280阅读 0赞

ByteBuf分类,从下面三个维度:

1、Pooled和Unpooled

2、Unsafe和非Unsafe

3、Heap和Direct

一、Pooled和Unpooled

这两个的区别是使用的内存是从预先分配好的内存还是未分配的内存取,第一个从预先分配好的内存去取一段连续的内存,第二种调用系统api直接去申请一块内存。

在分配bytebuf的时候,会有一个分配器:

70

分配器的两个子类实现UnpooledByteBufAllocator和PooledByteBufAllocator就能够实现pool和unpool内存的分配。后面我们会继续深入。

说得更加直白一点:pooled的ByteBuf基于内存池可以重复利用

二、Unsafe和非Unsafe

(通过指针)直接操作对象的内存地址还是通过安全的方式操作内存(直接操作物理内存),如果是unsafe就可以拿到bytebuf在jvm的内存调用jdk的unsafe直接去操作(通过jdk的API)。

我们之前知道,AbstractByteBuf会有个最终去执行getByte的方法 protected abstract byte _getByte(int index); 这些下划线开头的方法,最终都由他的子类去实现。我们先看一个子类的实现UnpooledUnsafeHeapByteBuf,他是一个unsafe的实现,看他实现的_getByte()方法:

  1. @Override
  2. protected byte _getByte(int index) {
  3. return UnsafeByteBufUtil.getByte(array, index);
  4. }

他是用的UnsafeByteBufUtil去实现的,我们后面会知道,所有的unsafebytebuf都会通过这个工具类去操作底层的unsafe,继续一直深入下去,就会看到是用底层的unsafe去实现的:

  1. static byte getByte(byte[] data, int index) {
  2. return UNSAFE.getByte(data, BYTE_ARRAY_BASE_OFFSET + index);
  3. }

然后我们看UnpooledUnsafeByteBuf的_getByte()的实现:

  1. @Override
  2. protected byte _getByte(int index) {
  3. return HeapByteBufUtil.getByte(array, index);
  4. }

他是通过HeapByteBufUtil去实现的,继续深入:

  1. static byte getByte(byte[] memory, int index) {
  2. return memory[index];
  3. }

对比他们两种实现,我们总结:unsafe通过操作底层unsafe的offset+index的方式去操作数据,非unsafe直接通过一个数组的下标(或者jdk底层的buffer)去操作数据

unsafe需要依赖到jdk底层的unsafe对象,非unsafe不需要。

三、Heap和Direct

操作jvm的堆内存还是直接内存,如果是direct内存不受jvm控制,所以也不会回收,需要自己去回收。

我们以UnpooledHeapByteBuf和UnpooledDirectByteBuf为例,介绍他们之间的区别。

首先看UnpooledHeapByteBuf源码:

  1. public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
  2. private final ByteBufAllocator alloc;
  3. byte[] array;
  4. private ByteBuffer tmpNioBuf;
  5. ...
  6. }

看到array,我们知道heap的话,它的数据就存放在array里面,所有内存相关的操作就在这个array上进行。上一部分我们知道,_getByte的时候,就是操作一个数组(通过数组下标的方式),这个数组就是这个array。

然后看UnpooledDirectByteBuf源码:

  1. */
  2. public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
  3. private final ByteBufAllocator alloc;
  4. private ByteBuffer buffer;
  5. private ByteBuffer tmpNioBuf;
  6. private int capacity;
  7. private boolean doNotFree;
  8. ...
  9. }

他是通过一个ByteBuffer来存储数据,ByteBuffer何许人也?

我们查看它的实现,能够看到有一个:DirectByteBuffer,这个是jdk的nio底层分配的buffer,用于操作堆外内存。

我们_getByte的时候,就是通过这个buffer去实现的:

  1. @Override
  2. protected byte _getByte(int index) {
  3. return buffer.get(index);
  4. }

内存分配就是通过Unpooled实现的。

现在我们分析unpooled,我们找到这个父类:

  1. public final class Unpooled {
  2. ...
  3. /**
  4. * Creates a new big-endian direct buffer with the specified {@code capacity}, which
  5. * expands its capacity boundlessly on demand. The new buffer's {@code readerIndex} and
  6. * {@code writerIndex} are {@code 0}.
  7. */
  8. public static ByteBuf directBuffer(int initialCapacity) {
  9. return ALLOC.directBuffer(initialCapacity);
  10. }
  11. ...
  12. }

我们可以知道,分配内存就是通过这个去实现的。继续看ALLOC的AbstractByteBufAllocator的实现:

  1. @Override
  2. public ByteBuf directBuffer() {
  3. return directBuffer(DEFAULT_INITIAL_CAPACITY, Integer.MAX_VALUE);
  4. }

继续看UnpooledByteBufAllocator的newDirectBuffer方法的实现:

  1. @Override
  2. protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
  3. ByteBuf buf = PlatformDependent.hasUnsafe() ?
  4. UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity) :
  5. new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
  6. return disableLeakDetector ? buf : toLeakAwareBuffer(buf);
  7. }

先说个题外话,我们看到是否unsafe不是我们自己决定的,而是由jdk实现的,如果能够获取到unsafe对象,就使用unsafe,反之亦然。然后我们分析的是UnpooledDirectByteBuf,继续:

  1. /**
  2. * Creates a new direct buffer.
  3. *
  4. * @param initialCapacity the initial capacity of the underlying direct buffer
  5. * @param maxCapacity the maximum capacity of the underlying direct buffer
  6. */
  7. protected UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
  8. super(maxCapacity);
  9. if (alloc == null) {
  10. throw new NullPointerException("alloc");
  11. }
  12. if (initialCapacity < 0) {
  13. throw new IllegalArgumentException("initialCapacity: " + initialCapacity);
  14. }
  15. if (maxCapacity < 0) {
  16. throw new IllegalArgumentException("maxCapacity: " + maxCapacity);
  17. }
  18. if (initialCapacity > maxCapacity) {
  19. throw new IllegalArgumentException(String.format(
  20. "initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
  21. }
  22. this.alloc = alloc;
  23. setByteBuffer(ByteBuffer.allocateDirect(initialCapacity));
  24. }

我们这个看到一个allocateDirect(initialCapacity),一层层进去,就能看到jdk的nio底层创建ByteBuffer的方法:

  1. public static ByteBuffer allocateDirect(int capacity) {
  2. return new DirectByteBuffer(capacity);
  3. }

然后通过setByteBuffer方法,把分配的内存设置到buffer变量里面去:

  1. private void setByteBuffer(ByteBuffer buffer) {
  2. ByteBuffer oldBuffer = this.buffer;
  3. if (oldBuffer != null) {
  4. if (doNotFree) {
  5. doNotFree = false;
  6. } else {
  7. freeDirect(oldBuffer);
  8. }
  9. }
  10. this.buffer = buffer;
  11. tmpNioBuf = null;
  12. capacity = buffer.remaining();
  13. }

总结,heap是依赖一个数组,direct是依赖jdk底层的ByteBuffer

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读

    相关 netty阅读ByteBuf

    今天我们开启新的篇章,netty很重要的内存概念将在这一章介绍。ByteBuf主要介绍以下几点: 1、内存与内存管理器的抽象 2、不同规格大小和不同类别的内存的分配策略