felixge / felixge/go-cgo-finalizer

incorrect assumption?

Open
#1 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
6
Forks
0
PR merge metrics
No merged PRs in 30d

Description

hello - thanks for the repo. However, thought I'd share something I noticed with you or anyone else reading over a decade later :). You're allocating memory into the Go heap [here](https://github.com/felixge/go-cgo-finalizer/blob/master/finalizer.go#L56). Thus to me it seems you're creating _go_ heap memory pressure, and this test program is not demonstrating that:

> yes, runtime.SetFinalizer() works well for automatically free()'ing cgo memory allocations!

In a real scenario, the C code would be allocating into the C heap. The Go GC only cares about the Go heap, so it's not going to see any memory pressure, GC will not run, and thus neither will the finalizers. Here's a [more realistic test script](https://gist.github.com/timruffles/48bfa3bd5a625d80e7c73d0eacfc8335) using `malloc` which demonstrates that you cannot rely on `runtime.SetFinalizer` to keep CGO memory usage within any defined bound.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.