Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xgb] Fix xgb intern to close replaced array #3558

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/main/java/ai/djl/ndarray/NDArrayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class NDArrayAdapter implements NDArray {

protected NDManager manager;
protected NDManager alternativeManager;
private NDArray alternativeArray;
protected NDArray alternativeArray;

protected Shape shape;
protected DataType dataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,31 @@ public ByteBuffer toByteBuffer(boolean tryDirect) {
/** {@inheritDoc} */
@Override
public void intern(NDArray replaced) {
if (handle != null && handle.get() != 0L) {
long pointer = handle.getAndSet(0L);
JniUtils.deleteDMatrix(pointer);
if (!(replaced instanceof XgbNDArray)) {
throw new IllegalArgumentException(
"The replaced NDArray must be an instance of XgbNDArray.");
}
XgbNDArray array = (XgbNDArray) replaced;
if (isReleased()) {
throw new IllegalArgumentException("This array is already closed");
}
if (replaced.isReleased()) {
throw new IllegalArgumentException("This target array is already closed");
}

long pointer = handle.getAndSet(0L);
JniUtils.deleteDMatrix(pointer);
if (alternativeArray != null) {
alternativeArray.close();
}

data = array.data;
handle = array.handle;
format = array.format;
alternativeArray = array.alternativeArray;
array.handle = null;
array.alternativeArray = null;
array.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory leak issue may caused by alternativeArray leak, I think we should close current array's alternative array as will. The code can be re-implemented as the following:

        if (!(replaced instanceof XgbNDArray)) {
            throw new IllegalArgumentException(
                    "The replaced NDArray must be an instance of XgbNDArray.");
        }
        XgbNDArray array = (XgbNDArray) replaced;
        if (isReleased()) {
            throw new IllegalArgumentException("This array is already closed");
        }
        if (replaced.isReleased()) {
            throw new IllegalArgumentException("This target array is already closed");
        }

        long pointer = handle.getAndSet(0L);
        JniUtils.deleteDMatrix(pointer);
        if (alternativeArray != null) {
            alternativeArray.close();
        }

        data = array.data;
        handle = array.handle;
        format = array.format;
        alternativeArray = array.alternativeArray;
        array.handle = null;
        array.alternativeArray = null;
        array.close();

}

/** {@inheritDoc} */
Expand Down
Loading