Search code examples
javadeep-learningresourcesdeeplearning4j

When to close DL4J INDArrays


I created a custom DataSetIterator. It works by randomly generating two INDArrays (one for input and one for output) in the next method and creating a DataSet out of it:

int[][] inputArray = new int[num][NUM_INPUTS];
int[][] expectedOutputArray = new int[num][];

for (int i = 0; i < num; i++) {//just fill the arrays with some data
    int sum = 0;
    int product = 1;
    for (int j = 0; j < inputArray[i].length; j++) {
        inputArray[i][j] = rand.nextInt();
        sum += inputArray[i][j];
        product *= inputArray[i][j];
    }
    expectedOutputArray[i] = new int[] { sum, product, sum / inputArray[i].length };
}

INDArray inputs = Nd4j.createFromArray(inputArray);//never closed
INDArray desiredOutputs = Nd4j.createFromArray(expectedOutputArray);//never closed
return new DataSet(inputs, desiredOutputs);

However, INDArray implements AutoClosable and the javadoc for close() states:

This method releases exclusive off-heap resources uses by this INDArray instance. If INDArray relies on shared resources, exception will be thrown instead PLEASE NOTE: This method is NOT safe by any means

  • Do I need to close the INDArrays?

  • If so, when do I need to close the INDArrays?

I have tried to use a try-with-resources but it threw an exception as the INDArray is closed when using it in the fit method.

The documentation of createFromArray(int[][]) does not seem to explain this.


Solution

  • you don't really need to close them. We take care of that automatically with javacpp. You can choose to close them but AutoCloseable was implemented for people who wanted more control over the memory management of the ndarrays.

    Edit: Javacpp is the underlying native integration that we use to connect to native libraries we maintain written in c++ and other libraries. All of our calculations and data are all based on native code and off heap memory. close() just forces us to de allocate those buffers faster. Javacpp has automatic de allocation built in to it already though.