Safely close INDArray


I am getting a SonarQube warning that I need to close an instance of an INDArray.

INDArray contingency = Nd4j.create(DataType.FLOAT, numUnique, numUnique);

I see that a close() method is implemented but looking at the docs I see:

PLEASE NOTE: This method is NOT safe by any means.

Is there a safe way to close this resource?


Close is basically optional. Usually we use javacpp’s GC to handle memory collection. We implemented a close method for people who want to control memory.

There are a few considerations:

  1. the javacpp nopointerGC knob. This prevents GC from running. This can be useful in cases where you want to control memory yourself.
  2. Workspaces: if you mix this with workspaces (eg: you do a try with with workspaces) you could end up with issues if you close ndarrays yourself

@wcneill could you elaborate on your use case a bit or is this question just because of the linter?

Good morning. I am asking because of the SonarQube linter. I don’t currently have any need of the advanced utility you mentioned but it’s good to know. Thank you.