How to free memory after required modules

Posted on
  • Is there a way to free memory for unused parts, loaded with require ?
    This could help for:

    • handle functions during startup only
    • create objects and remove them after use
    • dynamically load and unload fonts for display
    • ....
  • Sure, just use Modules.removeAllCached. There's another function that'll just remove a specific module too.

  • There may be a misunderstanding. I want to free memory.
    Starting with this:

    var g = require("ILI9341").connect(SPI2,C6,C7,C8­,function(){g.clear();});
    

    Process.memory() returns usage 329
    Dump shows var g = { ..... digitalWrite(C6,1....
    Result is as expected

    g = "";
    

    Process.memory() returns usage 331
    dump shows var g = ""; digitalWrite(C6,1.....
    I expected less memory usage

    Modules.removeAllCached(); 
    

    Process.memory() returns usage 330
    dump shows var g = ""; digitalWrite(C6,1.....
    I expected less memory usage
    Tried to do this:

    var b = "ILI9341";
    g = require(b).connect(....
    

    returns unable to read file no path
    Process.memory() still returns usage 332
    There is no object, no module, so how do I get rid of memory consumption ?

  • No misunderstanding - I think there might be a bug in Espruino - I'll look into it.

    If you try a different module it works as I suggested:

    >process.memory().usage
    =17
    >g=require("KeyPad")
    ={ ... }
    >process.memory().usage
    =61
    >g=""
    =""
    >process.memory().usage
    =62
    >Modules.removeAllCached()
    =undefined
    >process.memory().usage
    =19
    
  • Great, just tested nightly build and it works fine.

    What I'm doing now is to use something like this:

    1. copy my modules to sdcard in folder node_modules
    2. use require by variable not by string to avoid loading from WebIDE
    3. clear modules after running my init procedure

      var b,n;
      n = "myModule";
      b = require(n).connect(......
      // do a lot of functions
      b = ""; //to delete the object created during require
      Modules.removeCached(n);
      // do whatever is used after initialization
      

    Next test will be to split an application into modules which are loaded and unloaded by a main application.

  • Great! So are you actually running out of space, or is this 'just in case'?

    By the way, you can also do eval(require('fs').readFile("...")) which might be slightly easier if you're loading specific bits of code.

    Espruino is designed so that it can 'page' variables and code out to flash - potentially giving it massively more available memory. It could be a fun project for someone :)

  • I ran out of memory by creating an extended module for ILI9341 and adding simple charts, so called "PicoChart". SPI.send ran out of memory during processing.
    After restructering etc. there have been about 300 blocks left. Minimizing and some other changes brought it down to about 1000 blocks left.

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

How to free memory after required modules

Posted by Avatar for JumJum @JumJum

Actions