diff --git a/api/src/main/java/ai/djl/ndarray/BaseNDManager.java b/api/src/main/java/ai/djl/ndarray/BaseNDManager.java index ac3a049f59d..64b52355710 100644 --- a/api/src/main/java/ai/djl/ndarray/BaseNDManager.java +++ b/api/src/main/java/ai/djl/ndarray/BaseNDManager.java @@ -278,13 +278,7 @@ public void close() { } } for (TempResource resource : tempResources.values()) { - try { - if (!resource.detached) { - resource.resource.attach(resource.manager); - } - } catch (Exception e) { - logger.error("Temporary resource return failed.", e); - } + resource.returnResource(); } parent.detachInternal(uid); resources.clear(); @@ -307,7 +301,7 @@ public void debugDump(int level) { .append(") resource count: ") .append(resources.size()); - System.out.println(sb.toString()); // NOPMD + System.out.println(sb); // NOPMD for (AutoCloseable c : resources.values()) { if (c instanceof BaseNDManager) { ((BaseNDManager) c).debugDump(level + 1); @@ -316,6 +310,7 @@ public void debugDump(int level) { } protected static final class TempResource { + private NDResource resource; private NDManager manager; private boolean detached; @@ -325,5 +320,19 @@ public TempResource(NDResource resource, NDManager manager) { this.manager = manager; this.detached = false; } + + public void returnResource() { + try { + if (!detached) { + if (manager.isOpen()) { + resource.attach(manager); + } else { + resource.close(); + } + } + } catch (Exception e) { + logger.error("Temporary resource return failed.", e); + } + } } }