Javolution Struct allocations of arrays

483 Views Asked by At

Trying to allocate (direct memory) with Javolution.Struct that contains arrays, and the memory "explodes". No idea why, Would love to have a clue

Class A holds array of 50 of class B. Class B holds array of 1 million unsigned shorts:

import javolution.io.Struct;

public class Test {

    public static void main(String[] args) {
        Test t = new Test();
        t.work();
    }

    public void work() {
        A a = new A();
        System.out.println("a.size() = "+a.size()); // prints 100000000
    }
}

public class A extends Struct {
    public B[] randomName = array(new B[50]);
}

public class B extends Struct {
    public Unsigned16[] someData = array(new Unsigned16[1000000]);
}

The reason I need those arrays in one piece is the comfort of calling getByteBuffer() and getting the whole structure as single piece of memory, directly allocated in non jvm memory

The memory shows (10 GB) 6 GB instead of 100MB no clue why

[ javolution 6.1.0 ]

Edit 1: A more concise issue found to cause the same memory problem:

public Unsigned16[] someData = array(new Unsigned16[50000000]);

Allocating 50m unsigned16 in main (class needs to extend Struct obv) function is expected to have a 100mb memory footprint where its actually allocates 6gb

Edit 2: Its not happening if i'm calling the same line, without the wrapping with array() function:

public Unsigned16[] someData = new Unsigned16[50000000];

This is weird. I need the struct to contain this array, along with other Struct members (like Signed8 etc..), and when calling getByteBuffer() it will have the same piece of memory allocated and not pointers to different structs

Edit 3: This works as expected: ByteBuffer bb = ByteBuffer.allocateDirect(100*1000000); // allocates 100MB

I suspect there is a bug in Struct.array function in javolution lib. I will keep this post open until its fixed or someone post a workaround..

0

There are 0 best solutions below