DirectByteBuffer实现原理分析
1.创建DirectByteBuffer
Direct ByteBuffer是通过JNI在Java虚拟机外的内存中分配了一块(所以即使在运行时通过-Xmx指定了Java虚拟机的最大堆内存,还是可能实例化超出该大小的Direct ByteBuffer),该内存块并不直接由Java虚拟机负责垃圾收集.
使用allocateDirect()静态方法创建对象分配内存
ByteBuffer buffer=ByteBuffer.allocateDirect(256);
- 1 /**
- 2 * Allocates a new direct byte buffer.
- 3 *
- 4 * <p> The new buffer's position will be zero, its limit will be its
- 5 * capacity, its mark will be undefined, and each of its elements will be
- 6 * initialized to zero. Whether or not it has a
- 7 * {@link #hasArray </code>backing array<code>} is unspecified.
- 8 *
- 9 * @param capacity The new buffer's capacity, in bytes
- 10 * @return The new byte buffer
- 11 * @throws IllegalArgumentException If the <tt>capacity</tt> is a negative integer
- 12 */
- 13 public static ByteBuffer allocateDirect(int capacity) {
- 14 if (capacity < 0) {
- 15 throw new IllegalArgumentException("capacity < 0: " + capacity);
- 16 }
- 17
- 18 DirectByteBuffer.MemoryRef memoryRef = new DirectByteBuffer.MemoryRef(capacity);
- 19 return new DirectByteBuffer(capacity, memoryRef);
- 20 }
- 21 //
- 22 public MemoryRef(int capacity) {
- 23 VMRuntime runtime = VMRuntime.getRuntime();
- 24 buffer = (byte[]) runtime.newNonMovableArray(byte.class, capacity + 7);
- 25 allocatedAddress = runtime.addressOf(buffer);
- 26 // Offset is set to handle the alignment: http://b/16449607
- 27 offset = (int) (((allocatedAddress + 7) & ~(long) 7) - allocatedAddress);
- 28 isAccessible = true;
- 29 isFreed = false;
- 30 }
以上方法将创建一个容量为256字节的DirectByteBuffer,如果发现创建的缓冲区容量太小,唯一的选择就是重新创建一个大小合适的缓冲区.
DirectByteBuffer主要应用在android数据传递过程.减少数据与JNI数据拷贝转换操作
- DirectByteBuffer.putInt(value);
下面分析一下当执行putInt后,DirectByteBuffer都执行了什么操作
- 1 //计算写数据的位置
- 2 @Override
- 3 public final ByteBuffer putInt(int x) {
- 4 if (!memoryRef.isAccessible) {
- 5 throw new IllegalStateException("buffer is inaccessible");
- 6 }
- 7 if (isReadOnly) {
- 8 throw new ReadOnlyBufferException();
- 9 }
- 10 putInt(ix(nextPutIndex(SizeOf.INT)), x);
- 11 return this;
- 12 }
- 13 //调用Memory来完成Int数据存储
- 14 private ByteBuffer putInt(long a, int x) {
- 15 Memory.pokeInt(a, x, !nativeByteOrder);
- 16 return this;
- 17 }
- 18
- 19 private long ix(int i) {
- 20 return address + i;
- 21 }
- 22
再往下看Memory做了什么
- 1 public static void pokeInt(long address, int value, boolean swap) {
- 2 if (swap) {
- 3 value = Integer.reverseBytes(value);
- 4 }
- 5 pokeIntNative(address, value);
- 6 }
- 7 //因为最后执行到JNI层.这块就不涉及到字节序的问题
- 8 private static native void pokeIntNative(long address, int value);
当执行 DirectByteBuffer.getInt();都执行了哪些操作
- 1 public int getInt() {
- 2 if (!memoryRef.isAccessible) {
- 3 throw new IllegalStateException("buffer is inaccessible");
- 4 }
- 5 return getInt(ix(nextGetIndex(SizeOf.INT)));
- 6 }
- 7
- 8 //最后执行Memory 的JNI方法
- 9 private int getInt(long a) {
- 10 return Memory.peekInt(a, !nativeByteOrder);
- 11 }
- 12 //Memory 执行的操作
- 13 public static int peekInt(long address, boolean swap) {
- 14 int result = peekIntNative(address);
- 15 if (swap) {
- 16 result = Integer.reverseBytes(result);
- 17 }
- 18 return result;
- 19 }
- 20 private static native int peekIntNative(long address);
所以得出结论就本身在Java中引入DirectByteBuffer并不会提高性能
DirectByteBuffer实现原理分析的更多相关文章
- Java Reference核心原理分析
本文转载自Java Reference核心原理分析 导语 带着问题,看源码针对性会更强一点.印象会更深刻.并且效果也会更好.所以我先卖个关子,提两个问题(没准下次跳槽时就被问到). 我们可以用Byte ...
- Handler系列之原理分析
上一节我们讲解了Handler的基本使用方法,也是平时大家用到的最多的使用方式.那么本节让我们来学习一下Handler的工作原理吧!!! 我们知道Android中我们只能在ui线程(主线程)更新ui信 ...
- Java NIO使用及原理分析(1-4)(转)
转载的原文章也找不到!从以下博客中找到http://blog.csdn.net/wuxianglong/article/details/6604817 转载自:李会军•宁静致远 最近由于工作关系要做一 ...
- 原子类java.util.concurrent.atomic.*原理分析
原子类java.util.concurrent.atomic.*原理分析 在并发编程下,原子操作类的应用可以说是无处不在的.为解决线程安全的读写提供了很大的便利. 原子类保证原子的两个关键的点就是:可 ...
- Android中Input型输入设备驱动原理分析(一)
转自:http://blog.csdn.net/eilianlau/article/details/6969361 话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反 ...
- 转载:AbstractQueuedSynchronizer的介绍和原理分析
简介 提供了一个基于FIFO队列,可以用于构建锁或者其他相关同步装置的基础框架.该同步器(以下简称同步器)利用了一个int来表示状态,期望它能够成为实现大部分同步需求的基础.使用的方法是继承,子类通过 ...
- Camel运行原理分析
Camel运行原理分析 以一个简单的例子说明一下camel的运行原理,例子本身很简单,目的就是将一个目录下的文件搬运到另一个文件夹,处理器只是将文件(限于文本文件)的内容打印到控制台,首先代码如下: ...
- NOR Flash擦写和原理分析
NOR Flash擦写和原理分析 1. NOR FLASH 的简单介绍 NOR FLASH 是很常见的一种存储芯片,数据掉电不会丢失.NOR FLASH支持Execute On Chip,即程序可以直 ...
- 使用AsyncTask异步更新UI界面及原理分析
概述: AsyncTask是在Android SDK 1.5之后推出的一个方便编写后台线程与UI线程交互的辅助类.AsyncTask的内部实现是一个线程池,所有提交的异步任务都会在这个线程池中的工作线 ...
随机推荐
- DirectX12 3D 游戏开发与实战第六章内容
利用Direct3D绘制几何体 学习目标 探索用于定义.存储和绘制几何体数据的Direct接口和方法 学习编写简单的顶点着色器和像素着色器 了解如何用渲染流水线状态对象来配置渲染流水线 理解怎样创建常 ...
- 【百奥云GS专栏】全基因组选择之工具篇
目录 1. 免费开源包/库 1.1 R包 1.2 Python库 2. 成熟软件 3. WEB/GUI工具 前面我们已经介绍了基因组选择的各类模型,今天主要来了解一下做GS有哪些可用的软件和工具.基因 ...
- 如何构建自己的KEGG数据库
本文转自Y叔公众号 自己KEGG数据库好处: 可重复性好 没网也可以进行分析 步骤 1 在KEGG官网找到自己物种的3字符缩写 2 加载Y叔获取kegg.db 的R包 1 ##安装Y叔的包 2 lib ...
- C语言 序列反向互补函数
1 static char *revers(char *s) 2 { 3 int len=strlen(s); 4 char *s2=(char *)malloc(sizeof(char)*(len+ ...
- Excel-vlookup内部能不能用函数?(即内部嵌套函数)
11.vlookup(查找值,目标区域,列序号,FALSE0/TRUE1)内部能不能用函数?(即内部嵌套函数) 总结:只能说有,但不是所有,目前还没有找到规律(唯一的规律是内嵌函数结果值得是符合vlo ...
- 学习java 7.1
学习内容:数组的定义格式:int[ ] arr; int arr[ ]; 数组的动态初始化:int[ ] arr = new int[ ];静态初始化:int[ ] arr = new int[ ] ...
- zabbix之模板制作(memcache redis)
#:找一台主机安装redis和memcached(记得安装zabbix-agent) root@ubuntu:~# apt install redis root@ubuntu:~# apt insta ...
- transient关键字和volatile关键字
看到HashSet的源代码的时候,有一个关键字不太认识它..transient,百度整理之: Java的Serialization提供了一种持久化对象实例的机制,当持久化对象时,可能有一些特殊的对象数 ...
- Initialization of data members
In C++, class variables are initialized in the same order as they appear in the class declaration. C ...
- ActiveRecord教程
(一.ActiveRecord基础) ActiveRecord是Rails提供的一个对象关系映射(ORM)层,从这篇开始,我们来了解Active Record的一些基础内容,连接数据库,映射表,访问数 ...