Java, buffer lazily allocated, free-able on demand and addressed as byte []

Viewed 220

I am trying to implement a proof-of-concept memory-aware scheduling functionality by extending an existing Java program. The program uses buffers under the form of byte []. For my purpose byte [] are problematic because

  1. they are garbage collected
  2. they are allocated upfront instead of lazily (the JVM seems to touch all pages it has allocated when creating the buffer)
  3. they make the JVM allocate more and more memory which is not given back the OS.

To achieve my goal I would like buffers to be lazily allocated memory (pages allocated only when written to) and free-able on demand. This is similar to how it would happen in C++. In addition, as much as possible, I would like to minimize the changes to the existing code-base.

I looked at nio.ByteBuffer and at the Unsafe classes. Neither fits my case because

  1. java.nio.ByteBuffers don't seem to be lazily allocated. When I allocate an empty 1GB buffer the RSS of the program immediately goes to 1GB.
  2. Unsafe.allocateMemory is lazily allocated but I do not know how to reference it as byte [].

Is there any way to solve this? Any way to view memory allocated with Unsafe.allocateMemory() as a byte []? Or change an existing byte [] to point to memory allocated with Unsafe?

Thank you

1 Answers
Related