-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/cgo: using cgo when passing unsafe.Pointer of a slice to C function causes memory leak #40636
Comments
Can you show us exactly the commands you used to get that pprof result, so that we can reproduce it ourselves? Maybe unrelated to this issue, but this is almost certainly not what you want:
That's a pointer to the slice header. You want
That's a pointer to the first element of the slice. |
Your |
open browser. url is http://127.0.0.1:6060/debug/pprof/heap?debug=1
|
any sugguestion? |
Did you see this comment? I don't see a reply.
|
sorry i delete the |
Sorry, I wan't asking you to remove the func uploadMemLeak(wg *sync.WaitGroup) {
buf := make([]byte, 6)
etag_out := make([]byte, 256)
ptr := C.CString("1006")
c_err := C.cli_upload(ptr, (*C.uchar)(unsafe.Pointer(&buf)), (unsafe.Pointer(&etag_out[0])))
fmt.Printf("upload done, err = %d\n", int(c_err))
C.free(unsafe.Pointer(ptr))
wg.Done()
} What happens if you make that change? |
Thx I try your code in my env. Sadlly, there is still mem leak happened. i also tried deleting the package main
/*
#include <stdlib.h>
int cli_upload(char *file_data, unsigned char *file_data_buf, void *etag_out)
{
return 0;
}
*/
import "C"
import (
"bufio"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"unsafe"
)
func uploadMemLeak() {
buf := make([]byte, 6)
etag_out := make([]byte, 256)
ptr := C.CString("1006")
c_err := C.cli_upload(ptr, (*C.uchar)(unsafe.Pointer(&buf[0])), (unsafe.Pointer(&etag_out[0])))
fmt.Printf("upload done, err = %d\n", int(c_err))
C.free(unsafe.Pointer(ptr))
}
func main() {
fmt.Println(runtime.Version())
go http.ListenAndServe("0.0.0.0:6060", nil)
c := make(chan int)
reader := bufio.NewReader(os.Stdin)
_, _ = reader.ReadString('\n')
for i:=0; i<10000; i++ {
go uploadMemLeak()
}
reader = bufio.NewReader(os.Stdin)
_, _ = reader.ReadString('\n')
fmt.Printf("before gc\n")
runtime.GC()
fmt.Printf("end gc\n")
<-c
} there is still mem leak when i check the
In my code, mem leak happened at line 25 and 26. I don't know how to fix it. |
If you remove the Can you show us the code with the corrected use of |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?What did you do?
We found a memory leak when go pass a unsafe.pointer to a C function.
Code:
After running the code, we use pprof tool to checkout the heap memory.
we found memory leak at line 26 and 27.
What did you expect to see?
Memory recycled after C func
What did you see instead?
pprof result:
The text was updated successfully, but these errors were encountered: