-
but the espruino compiler isn't releasing memory when splice or shift.
it is interpreter, compilers are supposed to compile, not manage runtime memory
Just tried slice, splice, shift and also pop, at least in simple cases it works
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v19.182 (c) 2023 G.Williams Espruino is Open Source. Our work is supported only by sales of official boards and donations: http://espruino.com/Donate >a=[];i=0 =0 >process.memory() ={ free: 13978, usage: 22, total: 14000, history: 9, gc: 0, gctime: 2.469, blocksize: 18 } >for (var i=0;i<100;i++)a.push(i) =undefined >process.memory() ={ free: 13878, usage: 122, total: 14000, history: 13, gc: 0, gctime: 2.435, blocksize: 18 } >while(a.length)a.shift() =undefined >process.memory() ={ free: 13978, usage: 22, total: 14000, history: 16, gc: 0, gctime: 1.872, blocksize: 18 } >for (var i=0;i<100;i++)a.push(i) =undefined >process.memory() ={ free: 13878, usage: 122, total: 14000, history: 16, gc: 0, gctime: 1.664, blocksize: 18 } >for (i=0;i<100;i++)a.push(i) =undefined >process.memory() ={ free: 13778, usage: 222, total: 14000, history: 20, gc: 0, gctime: 0.697, blocksize: 18 } >while(a.length)a.shift() =undefined >process.memory() ={ free: 13978, usage: 22, total: 14000, history: 20, gc: 0, gctime: 2.424, blocksize: 18 } >for (i=0;i<100;i++)a.push(i) =undefined >process.memory() ={ free: 13878, usage: 122, total: 14000, history: 20, gc: 0, gctime: 2.41, blocksize: 18 } >while(a.length)a.pop() =undefined >process.memory() ={ free: 13978, usage: 22, total: 14000, history: 23, gc: 0, gctime: 0.525, blocksize: 18 } >for (i=0;i<100;i++)a.push(i) =undefined >a=a.slice(50) =[ 50, 51, 52, 53, 54, ... 95, 96, 97, 98, 99 ] >process.memory() ={ free: 13928, usage: 72, total: 14000, history: 25, gc: 0, gctime: 0.528, blocksize: 18 } >while(a.length)a.pop() =undefined >process.memory() ={ free: 13978, usage: 22, total: 14000, history: 25, gc: 0, gctime: 0.687, blocksize: 18 } > >a=[1,2,3,4,5] =[ 1, 2, 3, 4, 5 ] >process.memory() ={ free: 13973, usage: 27, total: 14000, history: 30, gc: 0, gctime: 0.812, blocksize: 18 } >a.splice(3,2,10,11) =[ 4, 5 ] >a =[ 1, 2, 3, 10, 11 ] >process.memory() ={ free: 13973, usage: 27, total: 14000, history: 30, gc: 0, gctime: 2.526, blocksize: 18 } >a.splice(3,2) =[ 10, 11 ] >process.memory() ={ free: 13975, usage: 25, total: 14000, history: 34, gc: 0, gctime: 2.294, blocksize: 18 } >a =[ 1, 2, 3 ] >
check
usage
, goes up and down as expectedIt will limit you from creating good stable apps
For me good and stable app is not bloated app that runs slow (=drains more battery) and needs lot of memory.
which are easy to maintain and extend. Most of the apps in the current app library are not extendable or maintainable.
This is debatable. Smaller code without unneeded abstractions can be easy to maintain and extend too. Someone else may see your perfectly testable and extensible code hard too. Also the size of typical watch app is not that big and people write it for fun and for free so extendable and maintainable may not be even the goal here.
At first I did just use an array but I need to limit the size otherwise the mem is very quickly consumed (think wanting to sample gps data, but only recent as you want to reduce noise). You are speed testing two different things. You are saying pushing to an array is quicker than pushing to an array and then pruning it. Which is obviously true. If you have a quicker way of pushing and then resizing that would be more constructive.
I am literally doing the same thing as splice after push (via shift). Originally I extended Array rather than having an internal array variable but the espruino compiler isn't releasing memory when splice or shift.
Ultimately you might save some cpu time with large chunks of repeated code. It will limit you from creating good stable apps which are easy to maintain and extend. Most of the apps in the current app library are not extendable or maintainable. You need to be able to split the code into small segments to unit test. Otherwise you are taking a step back to pre object orientated test driven development.