DirectByteBuffer内存回收笔记

今天在看netty源码时候又再次遇到了DirectByteBuffer,关于DirectByteBuffer的内存回收机制,在netty框架中被封装的面目全非,但其回收机制也是万变不离其宗,下面这几篇简单易懂的文章就介绍了DirectByteBuffer的概念极其内存回收方式,在这里和大家分享一下:

文章列表
jvm堆外内存–DirectByteBuffer
java之HeapByteBuffer&DirectByteBuffer以及回收DirectByteBuffer
NIO DirectByteBuffer 内存泄露的测试

关键的几行代码也黏贴在这,旨在加深印象
 public static void sleep(long i) {
        try {
            Thread.sleep(i);
        } catch (Exception e) {
            /*skip*/
        }
    }


    @Test
    public void testDirectByteBufferDeallocation() {

        /**
         DirectByteBuffer构造方法是包私有的,只能通过工具方法:
         public static ByteBuffer allocateDirect(int capacity)生成对象
         */
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024 * 1024 * 1024);


        System.out.println(byteBuffer.isDirect());
        sleep(1000 * 60);

        System.out.println("start clean");
        //可以通过操作系统命令查看堆外内存清理前后的内存占用
        clean(byteBuffer);
        System.out.println("end clean");

        sleep(1000 * 60);
    }


    public static void clean(final ByteBuffer byteBuffer) {
        if (byteBuffer.isDirect()) {
            ((DirectBuffer) byteBuffer).cleaner().clean();
        }
    }

lhever 

.---.                                                                         
|   |   .              __.....__   .----.     .----.   __.....__              
|   | .'|          .-''         '.  \    \   /    /.-''         '.            
|   |<  |         /     .-''"'-.  `. '   '. /'   //     .-''"'-.  `. .-,.--.  
|   | | |        /     /________\   \|    |'    //     /________\   \|  .-. | 
|   | | | .'''-. |                  ||    ||    ||                  || |  | | 
|   | | |/.'''. \\    .-------------''.   `'   .'\    .-------------'| |  | | 
|   | |  /    | | \    '-.____...---. \        /  \    '-.____...---.| |  '-  
|   | | |     | |  `.             .'   \      /    `.             .' | |      
'---' | |     | |    `''-...... -'      '----'       `''-...... -'   | |      
      | '.    | '.                                                   |_|      
      '---'   '---'  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值