sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.53k stars 138 forks source link

Issue about memory release #68

Closed hesilong closed 6 years ago

hesilong commented 6 years ago

hello! Recently I am using your api to call python methods. But I found a problem that has not been resolved, when I finished calling python's method. The memory did not come down. I'm not sure Whether my method is correct or not. The code is like belows: ` func (this TAiCall) SendSgfToPn(inMsg string, inBoardSize byte, outCArr C.CArray) {

if outCArr == nil {
    return
}

var (
    res   *python.PyObject
    sgf   string
    value C.float
    index int = 0
    cc    *python.PyObject 
)

sgf = inMsg

if inBoardSize == 19 {
    if TensorPn19Call == nil {
        return
    }
    bArgs := python.PyTuple_New(1)
    cc = PyStr(sgf)
    python.PyTuple_SetItem(bArgs, 0, cc)
    res = TensorPn19Call.Call(bArgs, python.Py_None)

    if res == nil {
        return
    }

}

if inBoardSize == 13 {
    if TensorPn13Call == nil {
        return
    }
    bArgs := python.PyTuple_New(1)
    cc = PyStr(sgf)
    python.PyTuple_SetItem(bArgs, 0, cc)
    res = TensorPn13Call.Call(bArgs, python.Py_None)

    if res == nil {
        return
    }

}

size := python.PyList_Size(res)

for i := 0; i < size; i++ {
    res2 := python.PyList_GetItem(res, i)
    insize := python.PyList_Size(res2)
    for j := 0; j < insize; j++ {
        finaldata := python.PyList_GetItem(res2, j)
        value = C.float(python.PyFloat_AsDouble(finaldata))
        C.SetCArrayValue(outCArr, C.int(index), unsafe.Pointer(&value), C._Float)
        index++
        finaldata.Clear()
    }
    res2.Clear()

}

res.Clear()

return

} ` And Memory Supervision is like this: result

From the picture, we can see the memory increases madly. Can the Clear Method release the memory that the List occupied or is there another method to realize the GC?

sbinet commented 6 years ago

I won't be able to debug this before next week. But you should look into the python C API and check what are the ownerships of the objects you create and who takes ownership of what. go-python exposes the python C API, with all its quirks, you can't make the economy of knowing its idioms.

hesilong commented 6 years ago

Thank you all the same! We'll try again! And If you have any idea later, please tell me !

hesilong commented 6 years ago

We just tested our code and I find that the memory leaks have nothing with your code! Thanks for your reply!