how to free menus memory #5724
Replies: 1 comment
-
Posted at 2023-02-07 by @fanoush
Well as long as the value is referenced by some variable it cannot be freed. however you should also clear however all those functions defined at line https://github.com/wagnerf42/gps/blob/main/app.js#L339 capture Posted at 2023-02-08 by wagnerf42 yeah that's what i thought. if i declare a global "street" variable, set it in the menu handler and use setInterval in the main to detect for the variable being set then it effectively frees the menu's memory. however i expected it to be also true if using a setTimeout in the handler itself since i thought the timeout's function would not be associated with the menu (the menu's handler registers the timeout and completes) and this is not the case. so i have the setInterval workaround which is fine, i'm not stuck but somehow it still don't get why the garbage collector is not freeing the memory if i use a timeout. Posted at 2023-02-08 by @fanoush
can you show the code with timeout and where would you expect the memory to be freed? Posted at 2023-02-08 by wagnerf42 yes, this guy : https://pastebin.com/D5k8NFhh is not freeing the memory. Posted at 2023-02-08 by @fanoush
I guess all this runs inside line 361 Posted at 2023-02-08 by @fanoush and btw the lines 340-355 could be moved out of the function and precomputed earlier? that could break the reference to the uncompressed data. Also are you aware that on line 339 you define new function for each cycle of the loop? so you get many duplicated function definitions, can you define function once earlier and just reuse it there (and just pass Posted at 2023-02-09 by @gfwilliams I haven't looked into your code I'm afraid, but when memory isn't freed in menus, I find it's often because of something like this:
The problem here is that If you use
Posted at 2023-02-09 by @fanoush @gfwilliams, I wonder how the interpreter works on this line https://github.com/wagnerf42/gps/blob/main/app.js#L339 Posted at 2023-02-09 by @gfwilliams
Yes, it does. But if the code if saved in flash, that new copy just references an address in flash, so it's not really using that much extra memory Posted at 2023-02-10 by @fanoush thanks, so it is just burning extra CPU in this case but memory is not affected as you probably need to pass one such function for each menu item anyway no matter how big or small. Anyway, I tried to learn how E.showMenu() works. So I started Bangle 2 emulator, copy pasted example from http://www.espruino.com/Reference#l_E_showMenu 'as is' and
Posted at 2023-02-10 by @fanoush and btw I am not that good about javascript and its scoping rules so I tried this
to see what value of BTW what is the function arguments for in the function callback? could I somehow access the menu item so that I know which item was clicked = get its name or even some precomputed value/id I could store there when creating the menu? For now I see some object with "draw' and "scroller" properties, nothing related to clicked item. The nearest solution is the syntax at line "four" - if the value is string type and I override the format method it shows just title and I get id/value in the onchange. But this is a bit fragile as it depends on the harmless string type of the value for now, if the value is integer or boolean it does more stuff behind scenes. Posted at 2023-02-20 by @gfwilliams Wow, thanks - that's a bit worrying. I've just put a fix in for that issue with
It's just to be able to access the underlying menu (the same thing that |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2023-02-07 by wagnerf42
hi,
i'm getting tight with my memory usage so I started profiling it a little bit.
i'm actually starting my app with a few menus and these guys require quite a lot of memory allocations: i need to decompress a few arrays using heatshrink.
now, after i select my entry in the menu i can safely free all this memory and live happily ever after.
i just need to keep a very tiny array i created in the callback.
however i'm not sure how to do that. i don't see how to wait in the main function for the menu to return so i usually launch my code in the menu entry's callback itself. if i do that however, all large arrays stay allocated. i tried to use a timeout to create a new task and return from the menu callback which works but does not free my memory.
i'm actually not even sure how the garbage collector is working.
i'm not sure i'm clear. my code is here : https://github.com/wagnerf42/gps/blob/main/app.js
i want to call the function on line 357 with the local street array and free the memory decompressed line 321.
Beta Was this translation helpful? Give feedback.
All reactions