简体   繁体   中英

How Java int[ ] array is implemented inside JVM?

I am trying to understand how JVM internally implements an array of primitive type, like int []

My question is in two parts:

1 - Does it use a ByteBuffer internally? Where can I find the source code and then perhaps change it according to my needs (to make a modified JVM of my own).

2 - Is there any way to trick javac to not use the build-in implementation of int [] but rather use an implementation provided by a library in lets say classpath -cp ? Is this possible and how?

My motivation is to declare this int [] in a memory outside of JVM (using allocateDirect() ) and access it outside from a native JNI code. This should avoid the memory copy overhead.

-B

  1. int[] is the primitive type. ByteBuffer may be based on an int[] but the opposite is certainly not true. It will almost cerainly be a word-aligned contiguous block of memory with 32 bit values stored in each word of the block. Several JVM's are open source, you can go look at the code if you really want to but it will be advanced stuff.

  2. There will be no easy way to do this.

It sounds like you are trying to do something in a very complicated way. It may be better to describe the actual problem you are trying to solve rather than asking about problems with an attempted solution to that problem.

Instead of using a Java IntBuffer or JNI, you can use sun.misc.Unsafe to allocate and access raw shared memory. This is Dangerous, but it is the absolute fastest way to access shared memory from Java and another process.

A helpful guide to the Unsafe methods is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM