You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I was playing around with gotch and ran a few tests to make sure I was dropping tensors and freeing memory correctly. However, even when I ran MustDrop(), memory usage always slowly creeped up. I'm running go 1.20 on Ubuntu 22.04 in WSL2. Here is a minimal example of when this happens.
I took a look at the underlying interface with libtorch and saw that most functions handled outputs of C functions by calling C.malloc(0).
funcRand(...) (...) {
ptr:= (*lib.Ctensor)(unsafe.Pointer(C.malloc(0)))
// Some C call that stores an allocated tensor at *ptr.retVal=&Tensor{ctensor: *ptr}
returnretVal, err
}
However, while we do free *ptr using MustDrop(), ptr is not freed. I think this is what is causing the memory leak.
By changing the above function to one of the two versions below, I was able to run the simple example without any memory leaks. I think the second one results in less Cgo calls, but I'm not sure if the usage is safe.
funcRand(...) (...) {
ptr:= (*lib.Ctensor)(unsafe.Pointer(C.malloc(0)))
deferfunc() { C.free(unsafe.Pointer(ptr)) }()
// Some C call that stores an allocated tensor at *ptr.retVal=&Tensor{ctensor: *ptr}
returnretVal, err
}
funcRand(...) (...) {
varuntypedPtruintptrptr:= (*lib.Ctensor)(unsafe.Pointer(&untypedPtr))
// Some C call that stores an allocated tensor at *ptr.retVal=&Tensor{ctensor: *ptr}
returnretVal, err
}
The text was updated successfully, but these errors were encountered:
Hello! I was playing around with
gotch
and ran a few tests to make sure I was dropping tensors and freeing memory correctly. However, even when I ranMustDrop()
, memory usage always slowly creeped up. I'm running go 1.20 on Ubuntu 22.04 in WSL2. Here is a minimal example of when this happens.I took a look at the underlying interface with
libtorch
and saw that most functions handled outputs of C functions by callingC.malloc(0)
.However, while we do free
*ptr
usingMustDrop()
,ptr
is not freed. I think this is what is causing the memory leak.By changing the above function to one of the two versions below, I was able to run the simple example without any memory leaks. I think the second one results in less Cgo calls, but I'm not sure if the usage is safe.
The text was updated successfully, but these errors were encountered: