最全Netty中的缓冲区为什么比原生NIO更高效,springcloud实战项目

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

Elasticsearch 24 题 +Memcached + Redis 40题:

Spring 26 题+ 微服务 27题+ Linux 45题:

Java面试题合集:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  1. API操作便捷性

  2. 动态扩容

  3. 多种ByteBuf实现

  4. 内存复用机制

  5. 零拷贝机制

ByteBuf的操作


三个重要属性:

  1. capacity容量

  2. readerIndex读取位置

  3. writerIndex写入位置

提供了两个指针变量来支持顺序读和写操作,分别是readerIndex和writeInDex,也就把缓冲区分成了三个部分:

0[ --已读可丢弃区域-- ]reaerIndex[ --可读区域-- ]writerIndex[ --待写区域-- ]capacity

复制代码

常用方法定义:

  • 随机访问索引getByte

  • 顺序读read*

  • 顺序写write*

  • 清除已读内容discardReadBytes

  • 清除缓冲区clear

  • 搜索操作

  • 标记和重置

  • 引用计数和释放

我们可以对这些api做一些测试,如下:

package io.netty.example.echo;

import java.util.Arrays;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

/**

  • @author daniel

  • @version 1.0.0

  • @date 2021/12/20

*/

public class ApiTest {

public static void main(String[] args) {

//1.创建一个非池化的ByteBuf,大小为10字节

ByteBuf buf = Unpooled.buffer(10);

System.out.println(“原始ByteBuf为:” + buf.toString());

System.out.println(“1.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//2.写入一段内容

byte[] bytes = {1,2,3,4,5};

buf.writeBytes(bytes);

System.out.println(“写入的bytes为:” + Arrays.toString(bytes));

System.out.println(“写入一段内容后ByteBuf为:” + buf);

System.out.println(“2.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//3.读取一段内容

byte b1 = buf.readByte();

byte b2 = buf.readByte();

System.out.println(“读取的bytes为:” + Arrays.toString(new byte[]{b1, b2}));

System.out.println(“读取一段内容后ByteBuf为:” + buf);

System.out.println(“3.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//4.将读取的内容丢弃

buf.discardReadBytes();

System.out.println(“丢弃已读取的内容后ByteBuf为:” + buf);

System.out.println(“4.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//5.清空读写指针

buf.clear();

System.out.println(“清空读写指针后ByteBuf为:” + buf);

System.out.println(“5.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//6.再次写入一段内容,比第一段内容少

byte[] bytes2 = {1,2,3};

buf.writeBytes(bytes2);

System.out.println(“再写入的bytes2为:” + Arrays.toString(bytes2));

System.out.println(“再写入一段内容后ByteBuf为:” + buf);

System.out.println(“6.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//7.将ByteBuf清空

buf.setZero(0, buf.capacity());

System.out.println(“内容清空后ByteBuf为:” + buf);

System.out.println(“7.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//8.再次写入一段超过容量的内容

byte[] bytes3 = {1,2,3,4,5,6,7,8,9,10,11};

buf.writeBytes(bytes3);

System.out.println(“写入超量的bytes3为:” + Arrays.toString(bytes3));

System.out.println(“写入超量内容后ByteBuf为:” + buf);

System.out.println(“8.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

}

}

复制代码

从这些api的使用中就可以体会到ByteBuf比ByteBuffer的强大之处,我们可以深入研究一下它在写入超量数据时的扩容机制,也就是buf.writeBytes(byte[])方法


ByteBuf动态扩容


容量默认值为256字节,最大值为Integer.MAX_VALUE,也就是2GB

实际调用AbstractByteBuf.writeBytes,如下:

AbstractByteBuf.writeBytes

@Override

public ByteBuf writeBytes(byte[] src) {

writeBytes(src, 0, src.length);

return this;

}

复制代码

AbstractByteBuf.writeBytes(src, 0, src.length);

@Override

public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {

ensureWritable(length); //检查是否有足够的可写空间,是否需要扩容

setBytes(writerIndex, src, srcIndex, length);

writerIndex += length;

return this;

}

复制代码

AbstractByteBuf.ensureWritable(length);

@Override

public ByteBuf ensureWritable(int minWritableBytes) {

ensureWritable0(checkPositiveOrZero(minWritableBytes, “minWritableBytes”));

return this;

}

复制代码

AbstractByteBuf.ensureWritable0(checkPositiveOrZero(minWritableBytes, “minWritableBytes”));

final void ensureWritable0(int minWritableBytes) {

final int writerIndex = writerIndex(); //获取当前写下标

final int targetCapacity = writerIndex + minWritableBytes; //计算最少需要的容量

// using non-short-circuit & to reduce branching - this is a hot path and targetCapacity should rarely overflow

if (targetCapacity >= 0 & targetCapacity <= capacity()) { //判断当前容量是否够用

ensureAccessible(); //检查ByteBuf的引用计数,如果为0则不允许继续操作

return;

}

if (checkBounds && (targetCapacity < 0 || targetCapacity > maxCapacity)) { //判断需要的容量是否是合法值,不合法为true直接抛出越界异常

ensureAccessible();//检查ByteBuf的引用计数,如果为0则不允许继续操作

throw new IndexOutOfBoundsException(String.format(

“writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s”,

writerIndex, minWritableBytes, maxCapacity, this));

}

// Normalize the target capacity to the power of 2.(标准化为2的次幂)

final int fastWritable = maxFastWritableBytes();

int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
alloc().calculateNewCapacity(targetCapacity, maxCapacity); //计算扩容后容量(只要扩容最小64)

// Adjust to the new capacity.

capacity(newCapacity); //设置新的容量

}

复制代码

alloc().calculateNewCapacity(targetCapacity, maxCapacity) -> AbstractByteBufAllocator

@Override

public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {

checkPositiveOrZero(minNewCapacity, “minNewCapacity”); //最小所需容量

if (minNewCapacity > maxCapacity) { //判断最小所需容量是否合法

throw new IllegalArgumentException(String.format(

“minNewCapacity: %d (expected: not greater than maxCapacity(%d)”,

minNewCapacity, maxCapacity));

}

final int threshold = CALCULATE_THRESHOLD; // 4 MiB page 阈值超过4M以其他方式计算

if (minNewCapacity == threshold) { //等于4M直接返回4M

return threshold;

}

// If over threshold, do not double but just increase by threshold.

if (minNewCapacity > threshold) { //大于4M,不需要加倍,只需要扩大阈值即可

int newCapacity = minNewCapacity / threshold * threshold;

if (newCapacity > maxCapacity - threshold) {

newCapacity = maxCapacity;

} else {

newCapacity += threshold;

}

return newCapacity;

}

// 64 <= newCapacity is a power of 2 <= threshold

final int newCapacity = MathUtil.findNextPositivePowerOfTwo(Math.max(minNewCapacity, 64)); //计算不少于所需容量的最小的2次幂的值

return Math.min(newCapacity, maxCapacity); //取容量所允许的最大值和计算的2次幂的最小值,当然在这儿就是newCapacity=64

}

复制代码

总结一下就是最小所需容量是否等于阈值,如果是直接返回阈值此后直接扩大阈值,否则以64为最小2次幂为基础每次扩大二倍直到阈值.


选择合适的ByteBuf实现


netty针对ByteBuf提供了8中具体的实现方式,如下:

| 堆内/堆外 | 是否池化 | 访问方式 | 具体实现类 | 备注 |

| — | — | — | — | — |

| heap堆内 | unpool | safe | UnpooledHeapByteBuf | 数组实现 |

| heap堆内 | unpool | unsafe | UnpooledUnsafeHeapByteBuf | Unsafe类直接操作内存 |

| heap堆内 | pool | safe | PooledHeapByteBuf | |

| heap堆内 | pool | unsafe | PooledUnsafeHeapByteBuf | ~ |

| direct堆外 | unpool | safe | UnpooledDirectByteBuf | NIO DirectByteBuffer |

| direct堆外 | unpool | unsafe | UnpooleUnsafedDirectByteBuf | ~ |

| direct堆外 | pool | safe | PooledDirectByteBuf | ~ |

| direct堆外 | pool | unsafe | PooledUnsafeDirectByteBuf | ~ |

在使用时,都是通过ByteBufAllocator分配器进行申请,同时分配器具有内存管理的功能。

在这儿堆内和堆外没有什么区别,对api的使用时一样的,仅仅是通过Unpooled申请的不一样.

那个safe和unsafe有什么区别呢?

以UnpooledHeapByteBuf和UnpooledUnsafeHeapByteBuf中的getByte(int index)方法为例进行分析

UnpooledHeapByteBuf

@Override

public byte getByte(int index) {

ensureAccessible();

return _getByte(index); //真正的获取字节的方法

}

@Override

protected byte _getByte(int index) {

return HeapByteBufUtil.getByte(array, index); //通过HeapByteBufUtil工具类获取数据

}

复制代码

最后

2020年在匆匆忙忙慌慌乱乱中就这么度过了,我们迎来了新一年,互联网的发展如此之快,技术日新月异,更新迭代成为了这个时代的代名词,坚持下来的技术体系会越来越健壮,JVM作为如今是跳槽大厂必备的技能,如果你还没掌握,更别提之后更新的新技术了。

更多JVM面试整理:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

tByte(int index) {

return HeapByteBufUtil.getByte(array, index); //通过HeapByteBufUtil工具类获取数据

}

复制代码

最后

2020年在匆匆忙忙慌慌乱乱中就这么度过了,我们迎来了新一年,互联网的发展如此之快,技术日新月异,更新迭代成为了这个时代的代名词,坚持下来的技术体系会越来越健壮,JVM作为如今是跳槽大厂必备的技能,如果你还没掌握,更别提之后更新的新技术了。

[外链图片转存中…(img-2tDeYp0J-1715578349802)]

更多JVM面试整理:

[外链图片转存中…(img-DyrUcQyN-1715578349803)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 12
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值