-
• #2
process.memory()
is probably the most important oneAlso check http://www.espruino.com/Internals
There is also E.dumpVariables or E.dumpFragmentation Alsotrace(variable)
can dump a tree of everything referenced byvariable
.There is no heap, just variables. Also there is CPU stack growing down, the value can be seen in
process.memory().stackEndAddress
so0x20000000+0x10000-process.memory().stackEndAddress
can give size of currently(?) used stack. -
• #3
Thanks a lot for your help! :)
I played withprocess.memory()
for a while on some toy code, but still no sure whatusage
andhistory
mean.
For example:const ramBefore = process.memory(); const helloWorld = `${JSON.stringify(process.memory())} Hello, world!`; const ramAfter = process.memory(); print(`RAM Before: ${JSON.stringify(ramBefore)}`); print(`RAM Middle: ${helloWorld}`); print(`RAM After: ${JSON.stringify(ramAfter)}`);
Result:
RAM Before: {"free":2383,"usage":117,"total":2500,"history":6,"gc":0,"gctime":3.47900390625,"blocksize":16,"stackEndAddress":536929592,"flash_start":0,"flash_binary_end":443732,"flash_code_start":446464,"flash_length":524288} RAM Middle: {"free":2336,"usage":164,"total":2500,"history":11,"gc":0,"gctime":3.57055664062,"blocksize":16,"stackEndAddress":536929592,"flash_start":0,"flash_binary_end":443732,"flash_code_start":446464,"flash_length":524288} Hello, world! RAM After: {"free":2330,"usage":170,"total":2500,"history":19,"gc":0,"gctime":3.57055664062,"blocksize":16,"stackEndAddress":536929592,"flash_start":0,"flash_binary_end":443732,"flash_code_start":446464,"flash_length":524288}
If I understand the document correctly, then
usage
is the number of currently used blocks of RAM, andhistory
is the number blocks used by all the the commands (JS statements) until inclusive the current statement, right? -
• #4
Another example of fibonacci:
print(`Initial: ${JSON.stringify(process.memory())}`); function fibonacci(num) { print(`num ${num}: ${JSON.stringify(process.memory())}`); if (num <= 1) return 1; return fibonacci(num - 1) + fibonacci(num - 2); } print(`Before: ${JSON.stringify(process.memory())}`); fibonacci(5); print(`After: ${JSON.stringify(process.memory())}`);
Result:
Initial: {"free":2379,"usage":121,"total":2500,"history":0} Before: {"free":2373,"usage":127,"total":2500,"history":0} num 5: {"free":2366,"usage":134,"total":2500,"history":0} num 4: {"free":2360,"usage":140,"total":2500,"history":0} num 3: {"free":2354,"usage":146,"total":2500,"history":0} num 2: {"free":2348,"usage":152,"total":2500,"history":0} num 1: {"free":2342,"usage":158,"total":2500,"history":0} num 0: {"free":2341,"usage":159,"total":2500,"history":0} num 1: {"free":2347,"usage":153,"total":2500,"history":0} num 2: {"free":2353,"usage":147,"total":2500,"history":0} num 1: {"free":2347,"usage":153,"total":2500,"history":0} num 0: {"free":2346,"usage":154,"total":2500,"history":0} num 3: {"free":2359,"usage":141,"total":2500,"history":0} num 2: {"free":2353,"usage":147,"total":2500,"history":0} num 1: {"free":2347,"usage":153,"total":2500,"history":0} num 0: {"free":2346,"usage":154,"total":2500,"history":0} num 1: {"free":2352,"usage":148,"total":2500,"history":0} After: {"free":2373,"usage":127,"total":2500,"history":0}
It's fun to see how the RAM usage grows, but I don't find how I can get the peak RAM usage of the program without putting
process.memory()
on every step... -
• #5
free+usage=total, history is command lines entered into console, available when pressing up key, those are cleared automatically when memory is low so are not counted into used space
-
• #6
Understood! Thanks a lot for your help :)
-
• #7
I don't find how I can get the peak RAM usage of the program without putting process.memory() on every step
That's what you'd have to do I'm afraid - there's no way to get peak RAM usage without doing that. It's a bit tricky to get a solid figure for that because of garbage collection.
-
• #8
Understood! Thanks :)
-
• #9
there is also a builtin debugger, try to run
debugger;
, it can single step your code andprint
command can evaluate anything between steps so I guess some plugin in webIDE could script that and log 'print process.memory().used' after every step. So it could be done without changing your code but would be slow to run it like that. -
• #10
Good idea! I tried
debugger;
withprint
manually. Looks like a good way. Will try some plugins or modifying the debugger code later to test on larger programs :D
Hello there,
Does anyone know how I can get the heap or RAM usage of a program on Pixl?
I'm trying to find out how much RAM or heap can be saved from applying the Performance Notes. Also trying to see how much RAM is used by the Espruino engine itself and by my program.
Any help will be appreciated.