resource consumption in a library

Posted on
  • Hi,

    I’m currently using a PUCKJS device, for which I have added a library for specific hardware support.
    The library is compiled and linked with Espruino.
    I am able to load the PUCK with this new code, and invoque some functions of this library from JavaScript code running on the Espruino WEB IDE.
    All of that works well, except that I can observe an abnormal memory consumption where all resources should be released.

    Basically, the library contains the following code in the jswrap source:

      "type" : "class",
      "class" : "MyContextClass"
        "type" : "staticmethod",
        "class" : "MYCLASS",
        "name" : "open",
        "generate" : "jswrap_myclass_open",
        "return" : ["JsVar", "A context object or null if any error"]
    JsVar *jswrap_myclass_open(void) {
        JsVar *context = jspNewObject(0, "MyContextClass");
        if (! context) {
            jsExceptionHere(JSET_ERROR, "myclass resource failure");
            return NULL;
        "type" : "method",
        "class" : "MyContextClass",
        "name" : "close",
        "generate" : "jswrap_myclass_close"
    void jswrap_myclass_close(JsVar *parent) {

    Then, under the WEB IDE, I repeatadly execute the following lines:

    myContext =;

    At each iteration, the process.memory displays a decreasing value for the "free" field and an increasing value for the "usage" field.

    Is there something missing or wrong in the close function that could explain why resource is consumed ?

    Thank you for your help.

  • In the close function you shouldn't be calling jsvUnLock(parent); - that might help?

    Basically you shouldn't unlock any objects that are passed in to a function or that are returned from one. It could be the extra unlock is causing the parent not to get freed.

    One good way to check - if your code can be compiled for desktop - is to build it to run on your PC with DEBUG=1. This will compile in assert statements (as well as checking for memory leaks on exit), and it's very likely that if anything strange is happening it'll tell you.

  • Thank you Gordon, you'r right, the problem disappears when I don't call jsvUnLock(parent);
    All works fine now.
    Thank you for your help.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

resource consumption in a library

Posted by Avatar for user75013 @user75013