博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bytebuffer vs c++ malloc
阅读量:5938 次
发布时间:2019-06-19

本文共 4111 字,大约阅读时间需要 13 分钟。

hot3.png

Java equivalents of malloc()newfree() and delete (ctd)

Continued from our introduction to  in C/C++ and Java.

A Java equivalent of the malloc() function...?

If you've read the previous page on  and  in Java, you may be wondering why the current section on malloc() even exists. We've just stated that in Java, all memory has to be accessed via well-defined objects. In C/C++, on the other hand, malloc() gives us a pointer to an arbitrary block of memory. And in Java, there's no such thing, right?

Well, strictly speaking this is true: there isn't a way in Java to access "raw" memory via its address (pointer)— at least, not in a way where the address is visible to your Java program. But Java does provide the following rough equivalents to an area of memory allocated via malloc():

  • if you just want a block of bytes, e.g. in order to buffer input form a file or stream, then a common solution is to use a byte array;
  • the Java NIO (New I/O) package provides various  that allow you to manipulate an array or area of memory more flexibly: with methods to get/set a given primitive at a particular offset in the buffer, for example.

So, for example, the equivalent of the following C code:

unsigned char *memarea = (char *) malloc(200);*(memarea + 10) = 200;*((int *) (memarea + 16) = 123456;ch = memarea[4];

would be something along the following lines in Java, using a  object:

ByteBuffer bb = ByteBuffer.allocate(200);bb.put(0, (byte) 200);bb.putInt(16, 123456);int ch = bb.get(4) & 0xff;

Notice a subtlety of buffer access in Java is that we must deal with sign conversions ourselves. To read an unsigned byte from the ByteBuffer, we must store it in an int (or some primitive big enough to handle values between 128 and 255— basically, anything but a byte!), and must explicitly "AND off" the bits holding the sign.

Performance

Note that in Sun's JVM— and probably other JVMs with a good JIT compiler— accesses to ByteBuffers usually compile directly to MOV instructions at the machine code level. In other words, accessing a ByteBuffer really is usually as efficient as accessing a malloced area of memory from C/C++1.

Direct byte buffers

In the above example, the byte buffer would actually be "backed" by an ordinary Java byte array. It is even possible to call bb.array() to retrieve the backing array. An alternative that is perhaps closer to malloc() is to create what is called a direct ByteBuffer in Java: a ByteBuffer that is backed by a "pure block of memory" rather than a Java byte array2. To do so requires us simply to change the initial call:

ByteBuffer bb = ByteBuffer.allocateDirect(200);

Note that from Java, we still don't see or have control over the actual memory address of the buffer. Direct byte buffers have the advantage ofaccessibility via native code: although the address is of the buffer isn't available or controllable in Java, it is possible for your Java program to interface with native code (via the Java Native Interface). From the native side, you can:

  • query the address of a direct buffer;
  • allocate a direct buffer around a determined address range.

This very last point is quite significant, as it effectively allows things like device drivers to be written in Java (OK, in "Java plus a couple of lines of C" at any rate...).

Note, however, that you generally shouldn't use direct ByteBuffers "by default":

 

  • because they're essentially not Java objects (or at least, the actual data isn't), the direct buffer data is not allocated from the Java heap, but rather from the process's remaining memory space;
  • in practice, direct buffers are difficult to garbage collect, if even possible: Sun's Java 1.4 VM, for example, appears never to reclaim the memory of a direct byte buffer.

See also:

23141705_mTsL.gif 
23141705_mTsL.gif 
23141705_mTsL.gif 
23141705_mTsL.gif 

1. The same is often true of accessing object fields: the JIT compiler can compile accesses to object fields into MOV instructions that "know" the offset of the field in question.

2. You can actually create buffers backed by other types of primitive array, such as an IntBuffer backed by an int array.

 

--------------------\

ref:

转载于:https://my.oschina.net/fuckmylife0/blog/1554853

你可能感兴趣的文章
深度解读 - Windows 7核心图形架构细致分析(来自微软)
查看>>
微软软件开发技术二十年回顾-API篇(转)
查看>>
多行文本溢出显示省略号
查看>>
[Android Pro] http请求中传输base64出现加号变空格的解决办法
查看>>
Simpson公式的应用(HDU 1724/ HDU 1071)
查看>>
逆波兰表达式
查看>>
[BeiJing2006]狼抓兔子
查看>>
[SDOI2011]染色
查看>>
单页应用和多页应用
查看>>
jsp 页面导出excel时字符串数字变成科学计数法的解决方法
查看>>
docker
查看>>
sql 删除约束
查看>>
操作系统-输入输出系统
查看>>
C#引用非托管.dll
查看>>
MySQL 自适应哈希索引
查看>>
Markdown使用Demo
查看>>
跳过丢失归档进行恢复
查看>>
iOS 最新公布app到AppStore全流程具体解释
查看>>
正面模式(门面模式)-鼠标画地图实例
查看>>
项目中使用的spring 注解说明
查看>>