-
Notifications
You must be signed in to change notification settings - Fork 669
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,14 +88,36 @@ 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 == null) { | ||
throw new IllegalArgumentException("The replaced NDArray cannot be null."); | ||
} | ||
if (!(replaced instanceof XgbNDArray)) { | ||
throw new IllegalArgumentException( | ||
"The replaced NDArray must be an instance of XgbNDArray."); | ||
} | ||
XgbNDArray array = (XgbNDArray) replaced; | ||
data = array.data; | ||
handle = array.handle; | ||
format = array.format; | ||
|
||
synchronized (this) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NDArray operation is not thread-safe. Adding synchronized here won't improve thread-safty |
||
if (handle != null && handle.get() != 0L) { | ||
long pointer = handle.getAndSet(0L); | ||
JniUtils.deleteDMatrix(pointer); | ||
} | ||
|
||
data = array.data; | ||
format = array.format; | ||
|
||
if (array.handle != null && array.handle.get() != 0L) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this null check, if we want to prevent a closed NDArray, we should just do:
|
||
if (handle == null) { | ||
handle = new AtomicLong(); | ||
} | ||
handle.set(array.handle.getAndSet(0L)); | ||
} | ||
} | ||
|
||
array.data = null; | ||
array.handle = null; | ||
array.format = null; | ||
array.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
} | ||
|
||
/** {@inheritDoc} */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessary, the
instanceof
will check null: