Should I call close()?

I’m running my nd4j app and I can’t see in the metrics that the memory usage only goes up and never down.

Should I call .close() on every matrix I create? When should I use it? I suspect that maybe the matrices are not released from memory. Applies for the CPU windows & Linux backends

Memory is only released automatically if:

  1. You don’t hold on to it (i.e. don’t keep a reference to it)
  2. Garbage Collection actually runs.

GC can end up running too infrequently because the native memory usage doesn’t count towards heap memory usage.

If you are in a situation like that, calling .close() can help.

The problem is, it’s not that the memory is released after some time, it’s not released at all. It doesn’t seem like the GC frequency is the problem, it seems like nd4j doesn’t release the memory. Calling close on each matrix will make the code very ugly, as I have a long flow with many matrices

If GC runs but the memory isn’t freed, that means you are holding on to a reference somewhere. That might happen because you hold on to a DataSet object for example.

I only use INDArray in a method whose input and output are normal POJOS.
I am using apache flink as my framework, is there a chance flink’s memory management interferes with the memory and doesn’t release it?
Thanks for your comment :slight_smile: