Merge pull request #112 from pjongy/minor_cleanup_memory

MINOR: Move runtime.SetFinalizer into freeXXX
This commit is contained in:
sugarme 2023-10-19 16:10:39 +11:00 committed by GitHub
commit e0fce090d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 22 deletions

View File

@ -17,8 +17,12 @@ type Scalar struct {
// free releases C allocated memory.
func freeCScalar(x *Scalar) error {
nbytes := x.nbytes()
atomic.AddInt64(&AllocatedMem, -nbytes)
if gotch.Debug {
nbytes := x.nbytes()
atomic.AddInt64(&AllocatedMem, -nbytes)
log.Printf("INFO: Released scalar %q - C memory: %d bytes.\n", x.name, nbytes)
}
lock.Lock()
delete(ExistingScalars, x.name)
lock.Unlock()
@ -28,10 +32,6 @@ func freeCScalar(x *Scalar) error {
return err
}
if gotch.Debug {
log.Printf("INFO: Released scalar %q - C memory: %d bytes.\n", x.name, nbytes)
}
return nil
}
@ -52,17 +52,17 @@ func newScalar(cscalar lib.Cscalar, nameOpt ...string) *Scalar {
}
atomic.AddInt64(&ScalarCount, 1)
nbytes := x.nbytes()
atomic.AddInt64(&AllocatedMem, nbytes)
if gotch.Debug {
nbytes := x.nbytes()
atomic.AddInt64(&AllocatedMem, nbytes)
log.Printf("INFO: scalar %q added - Allocated memory (%d bytes).\n", x.name, nbytes)
}
lock.Lock()
x.name = newName(nameOpt...)
ExistingScalars[x.name] = struct{}{}
lock.Unlock()
if gotch.Debug {
log.Printf("INFO: scalar %q added - Allocated memory (%d bytes).\n", x.name, nbytes)
}
runtime.SetFinalizer(x, freeCScalar)
return x

View File

@ -66,8 +66,12 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor {
x.d = new(bigStruct)
atomic.AddInt64(&TensorCount, 1)
nbytes := x.nbytes()
atomic.AddInt64(&AllocatedMem, nbytes)
if gotch.Debug {
nbytes := x.nbytes()
atomic.AddInt64(&AllocatedMem, nbytes)
log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes)
}
lock.Lock()
name := newName(nameOpt...)
if _, ok := ExistingTensors[name]; ok {
@ -78,10 +82,6 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor {
x.name = name
if gotch.Debug {
log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes)
}
x.calledFrom = "newTensor()"
runtime.SetFinalizer(x, freeCTensor)
@ -170,6 +170,10 @@ func freeCTensor(ts *Tensor) error {
// IMPORTANT. make it nil so won't double free.
ts.ctensor = nil
// Clear SetFinalizer on ts so no double free tensor.
// Ref. https://pkg.go.dev/runtime#SetFinalizer
runtime.SetFinalizer(ts, nil)
return nil
}
@ -1190,10 +1194,6 @@ func (ts *Tensor) Drop() error {
return nil
}
// Clear SetFinalizer on ts so no double free tensor.
// Ref. https://pkg.go.dev/runtime#SetFinalizer
runtime.SetFinalizer(ts, nil)
ts.calledFrom = "ts.Drop()"
return freeCTensor(ts)
}