Buffer vs Unsafe - Outside JVM

<p>I have a requirement to use a space in the available RAM which the GC has no control on. I read a few articles on the same which gave me the introduction on two approaches. They are specified in the following code.</p><pre><br /><code>package com.directmemory;<br /></code><br /></pre><p>import java.lang.reflect.Field; import java.nio.ByteBuffer;</p><p>import sun.misc.Unsafe;</p><p>public class DirectMemoryTest {</p><pre><br /><code>public static void main(String[] args) {<br /><br /> //Approach 1<br /> ByteBuffer directByteBuffer = ByteBuffer.allocateDirect(8);<br /> directByteBuffer.putDouble(1.0);<br /> directByteBuffer.flip();<br /> System.out.println(directByteBuffer.getDouble());<br /><br /> //Approach 2<br /> Unsafe unsafe = getUnsafe();<br /> long pointer = unsafe.allocateMemory(8);<br /> unsafe.putDouble(pointer, 2.0);<br /> unsafe.putDouble(pointer+8, 3.0);<br /><br /> System.out.println(unsafe.getDouble(pointer));<br /> System.out.println(unsafe.getDouble(pointer+8));<br /> System.out.println(unsafe.getDouble(pointer+16));<br />}<br /><br />public static Unsafe getUnsafe() {<br /> try {<br /> Field f = Unsafe.class.getDeclaredField("theUnsafe");<br /> f.setAccessible(true);<br /> return (Unsafe) f.get(null);<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> return null;<br />}<br /></code><br /></pre><p>}</p><p>I have a couple of questions</p><p>1) Why should I ever pay attention to Approach 1 mentioned in the code because as per my understanding ByteBuffer.allocateDirect() cannot return me a buffer with a storage capacity of greater than 2GB ? So if my requirement is to store say 3 GB of data, I have to create a new buffer and store the data there which would mean that apart from storing data I have additional responsibility of identifying the respective buffer (out of the list of 'n' buffers) which maintains a pointer to direct memory.</p><p>2) Isn't approach 2 a little faster than approach 1 because I don't have to first find the buffer and then the data, I just need an indexing mechanism of for an object's field and use getDouble/getInt methods and pass the absolute address ?</p><p>3) Is the allocation of direct memory (is it right to say off heap memory ?) related to a PID ? If on a machine, I have 2 java processes, allocateMemory calls in PID 1 and PID 2 give me never intersecting memory blocks to use ?</p><p>4) Why is the last sysout statement not resulting in 0.0 ? The idea is that every double uses 8 bytes so I store 1.0 at address returned by allocateMemory say address = 1, the 2.0 at address 1+8 which is 9 and then stop. So shouldn't the default value be 0.0 ?</p><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34469568/buffer-vs-unsafe-outside-jvm">http://ift.tt/1OlveY3</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading