Comments (6)
messageDict.Clear()
will clear the content of the dict (ie: remove (eventually) its entries) but memory will still be retained by the backing structure of the dict
.
you need to call PyObject.Decref()
on it:
func main() {
initPython()
for {
//Infinite loop, make some data and create python dict
message := createMessage()
messageDict := getPyDict(message)
messageDict.Clear()
messageDict.Decref()
}
}
that's because PyDict_New()
returns a new reference that should be owned by the calling code (and the calling code should take care of disposing it after use.)
see:
https://docs.python.org/2/c-api/intro.html#objects-types-and-reference-counts
hth,
-s
(feel free to reopen if something's astray)
from go-python.
Nope. Made no difference. Still the same, even after enabling Python GC manually via their API https://docs.python.org/2/library/gc.html (called gc.enable() function).
from go-python.
you're right. there are 2 other errors, leading to memory leaks:
- you need to call
PyDict_Clear(messageDict)
and notmessageDict.Clear()
- you need to call
pyKey.DecRef()
andpyValue.DecRef()
asPyDict_SetItem
does not steal references
with these modifications, the following program:
package main
import python "github.com/sbinet/go-python"
func main() {
initPython()
for {
//Infinite loop, make some data and create python dict
message := createMessage()
messageDict := getPyDict(message)
python.PyDict_Clear(messageDict)
messageDict.DecRef()
}
}
func createMessage() map[string]interface{} {
payload := make(map[string]interface{})
payload["key_1"] = "value_1"
payload["key_2"] = "value_2"
payload["key_3"] = "value_3"
return payload
}
func getPyDict(message map[string]interface{}) *python.PyObject {
messageDict := python.PyDict_New()
for key, value := range message {
pyKey := python.PyString_FromString(key)
pyValue := python.PyString_FromString(value.(string))
python.PyDict_SetItem(messageDict, pyKey, pyValue)
pyKey.DecRef()
pyValue.DecRef()
}
return messageDict
}
func initPython() {
//Init Python
if err := python.Initialize(); err != nil {
panic(err)
}
}
stabilizes at 12.5m
RSS on my 64b linux machine.
from go-python.
Cool. Works !!!. Thanks a lot, have been struggling with this for a while. I see that there is no corresponding function PyDict_Clear()
available for Lists or Tuples. Does that mean that Clear()
and subsequent DecRef()
on the objects would be sufficient?
from go-python.
yes.
from go-python.
Noticed that Clear()
infact calls DecRef()
on the object after setting the pointer to null. So calling Clear()
was enough for all other objects except for the dict where I had to make another extra call to PyDict_Clear()
.
from go-python.
Related Issues (20)
- Does it works on Centos7 i686 system? HOT 1
- go get report errors HOT 4
- panic when register callback from go into python as module HOT 1
- How to call a function of Go from python HOT 3
- After running go-python script for a while, the memory becomes larger and larger HOT 1
- How to build the environment required by go-golang under window
- panic when run multi goroutine HOT 3
- Error reporting for execute command under win10 HOT 3
- Hi,
- Question: Usable with WebAssembly (WASM)? HOT 1
- Documentation not displayed due to license restrictions. HOT 2
- Pandas and go Python are incompatible? HOT 9
- D:\go\project\src\github.com\sbinet\go-python\capi.go:33:32: could not determine kind of name for C.METH_OLDARGS
- Exception SystemError: '/builddir/build/BUILD/Python-2.7.5/Objects/classobject.c:521: bad argument to internal function'
- can i import python module like selenium to use inside golang code?
- Let's try it, python bindings cgo-less HOT 1
- Cannot pass Integer type to CallFunction
- go panic when multi thread HOT 1
- How to use cPickle loads or dump complicated data, like array, class object
- memory leak๏ผ HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-python.