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

do dispose in NDArray.finalize() #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,20 @@ object NDArray {
* WARNING: it is your responsibility to clear this object through dispose().
* </b>
*/
// scalastyle:off finalize
class NDArray private[mxnet](private[mxnet] val handle: NDArrayHandle,
val writable: Boolean = true) extends WarnIfNotDisposed {
val writable: Boolean = true) {
// record arrays who construct this array instance
// we use weak reference to prevent gc blocking
private[mxnet] val dependencies = mutable.HashMap.empty[Long, WeakReference[NDArray]]
private var disposed = false
def isDisposed: Boolean = disposed

override protected def finalize(): Unit = {
dispose()
super.finalize()
}

def serialize(): Array[Byte] = {
val buf = ArrayBuffer.empty[Byte]
checkCall(_LIB.mxNDArraySaveRawBytes(handle, buf))
Expand Down Expand Up @@ -931,6 +937,7 @@ class NDArray private[mxnet](private[mxnet] val handle: NDArrayHandle,
}

def internal: NDArrayInternal = {
waitToRead()
val myType = dtype
val arrLength = DType.numOfBytes(myType) * size
val arr = Array.ofDim[Byte](arrLength)
Expand Down Expand Up @@ -1017,6 +1024,7 @@ class NDArray private[mxnet](private[mxnet] val handle: NDArrayHandle,
shape.hashCode + toArray.hashCode
}
}
// scalastyle:on finalize

private[mxnet] object NDArrayConversions {
implicit def int2Scalar(x: Int): NDArrayConversions = new NDArrayConversions(x.toFloat)
Expand Down