joshuaswaney opened a new issue #17469: Java NDArray Disposal
URL: https://github.com/apache/incubator-mxnet/issues/17469
 
 
   ## Description
   It appears that neither ResourceScope nor NDArrayCollector work fully as 
intended in Java.
   
   Following [the ResourceScope 
documentation](https://github.com/apache/incubator-mxnet/blob/master/scala-package/memory-management.md),
 it correctly disposes of NDArray objects created within the block but **not** 
for the ones returned by `Predictor.predictWithNDArray`:
   ```java
   NDArray input;
   NDArray result;
   Predictor predictor;
   
   try (ResourceScope r = new ResourceScope()) {
     input = NDArray.array(...);
     List<NDArray> results = predictor.predictWithNDArray(Arrays.asList(input));
     result = results.get(0);
   }
   input.nd().isDisposed(); // true
   input.nd().isDeAllocated(); // true
   result.nd().isDisposed(); // false
   result.nd().isDeAllocated(); // false
   ```
   
   Following [the NDArrayCollector 
documentation](https://github.com/apache/incubator-mxnet/blob/master/scala-package/core/src/main/scala/org/apache/mxnet/NDArrayCollector.scala#L54-L65),
 it does not dispose of any NDArray objects.  It looks like NDArrayCollector is 
deprecated but it's not clear that this is the reason.
   ```java
   NDArray[] outerResult = NDArrayCollector.auto().withScope(new 
scala.runtime.AbstractFunction0<Object>() {
     @Override
     public Object apply() {
       NDArray input = NDArray.array(...);
       List<NDArray> results = 
predictor.predictWithNDArray(Arrays.asList(input));
       NDArray result = results.get(0);
       return new NDArray[]{input, result};
     }
   });
   outerResult[0].nd().isDisposed(); // false
   outerResult[0].nd().isDeAllocated(); // false
   outerResult[1].nd().isDisposed(); // false
   outerResult[1].nd().isDeAllocated(); // false
   ```
   
   ## What have you tried to solve it?
   I'm able to work around this by manually disposing NDArray objects using 
`dispose()` on each one.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to