0

ByteBuffer says that it could be either direct or non-direct. Given a direct byte buffer, the JVM will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.

Is this similar to Registers in C?

int main()
{
  register int i = 10;
  int *a = &i;
  printf("%d", *a);
  getchar();
  return 0;
}

Where the value is stored directly in the register instead of loading it to memory.

Tom Taylor
  • 2,887
  • 2
  • 34
  • 55
  • Possible duplicate of [ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()](https://stackoverflow.com/questions/5670862/bytebuffer-allocate-vs-bytebuffer-allocatedirect) – Robert Rouhani Jul 24 '18 at 18:48
  • 2
    No @RobertRouhani. I'm aware of `ByteBuffer` `allocate` vs `allocateDirect`. But my doubt is whether `direct` and `register` in C are similar.. – Tom Taylor Jul 24 '18 at 18:49
  • 1
    Means does `ByteBuffer` does not allocate memory in heap? What exactly process memory space mean here? – Tom Taylor Jul 24 '18 at 18:56
  • The JVM asks the OS for a block of memory that it can lock, rearrange, and use however it wants. Almost everything you allocate in Java is memory contained inside this block. The OS itself does something similar, but with a block of memory for each process. ByteBuffer is one of the few (if not only) ways of asking the OS for another block of memory outside of what the JVM asked for when it started up. – Robert Rouhani Jul 24 '18 at 18:59
  • The main benefit and reason they exist is so that you can use the JNI to pass memory around to code written in other languages (typically C/C++) without having to ask the JVM to lock a section of it's heap for you, or to make a full copy of the buffer before passing it to native code. This is all described in the question I marked this as a duplicate of (which is why I marked it as duplicate) – Robert Rouhani Jul 24 '18 at 19:01
  • "asking the OS for another block of memory outside of what the JVM asked for when it started up" - Means who deallocates (or) free's the memory allocated something like this? Is this the job of the OS to do garbage collection on these type of objects? – Tom Taylor Jul 24 '18 at 19:02
  • @aglassman : I hope we can have these technical discussions here.. – Tom Taylor Jul 24 '18 at 19:05
  • 1
    I would say the C equivalent of DirectByteBuffer is "all variables". – Lee Daniel Crocker Jul 24 '18 at 19:21

0 Answers0