Code Monkey home page Code Monkey logo

Comments (6)

sbinet avatar sbinet commented on June 12, 2024

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.

mpoornima avatar mpoornima commented on June 12, 2024

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.

sbinet avatar sbinet commented on June 12, 2024

you're right. there are 2 other errors, leading to memory leaks:

  • you need to call PyDict_Clear(messageDict) and not messageDict.Clear()
  • you need to call pyKey.DecRef() and pyValue.DecRef() as PyDict_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.

mpoornima avatar mpoornima commented on June 12, 2024

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.

sbinet avatar sbinet commented on June 12, 2024

yes.

from go-python.

mpoornima avatar mpoornima commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.