Memory Management #500
Replies: 14 comments
-
Posted at 2014-11-21 by @gfwilliams
Good spot :) I'll update the docs, but it's max 4 characters. Any more and another variable will be created (which will get you up to 16 characters), and more means another variable, and so on. So if you keep your function names and variable names at 4 characters or less, you may see some improvements even without minification...
At the moment, there is... This is related to your question about defining functions inside functions. The problem with putting one function inside another is that because Espruino preserves the text inside the function, it's actually got one copy of the function as text, and then another copy as an actual function when the code is executed. Even if you delete the reference to the original function, I believe it will still be referenced from the functions that were defined inside it, so it won't get removed. So defining in the prototype uses a few more variables, but the actual text definition of the function is thrown away after the function is put in the prototype so you end up saving space. It's something I have plans to fix in the future, by implementing function hoisting which would scan a function (on definition) for the functions defined inside it, and would then pull them out as separate variables. It's a bit of a pain though, because Espruino would have to reconstruct what the code when it had to convert the function to a string :) So I'm afraid for the moment you're a bit stuck. At some point I have to stop using the online closure compiler for minification, and stick one inside the Web IDE. When I do that then I'd really like to use something that understood about modules, and could actually optimise over that boundary (removing functions that weren't being used and shortening all names). If anyone's got any ideas about that then I'd love to hear them though :) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-21 by @gfwilliams Ok, quick update - I was wrong. As an example:
So the function that has been executed gets completely removed (even though its execution context remains). This means that it's best to do the following (which will allow the minifier to work its magic):
That doesn't work so well for modules though I'm afraid, because the functions that are defined still need the 'module scope', which references the function that defined them. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-21 by @gfwilliams You might be able to do:
I haven't tried it - but that really is quite nasty! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-21 by DaveNI Thanks for such a quick, comprehensive answer - I'll try some of this out later. I actually quite enjoy having limited ram- it forces me to think about every function in detail and can result in a much better end product. With unlimited resources code can become bloated really quickly. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-21 by DaveNI @gordon : That worked out perfectly, just what I needed: A couple of extra lines of code saved me about 100 variable blocks!! AND i can use long/descriptive variable & function names since the minifier is now free to do its magic!! Anyone having "out of memory" issues should try this pattern:
I am now going to try it on a few of my modules to see if it helps there too. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-21 by DaveNI Quick Update - I'm now up o 950 free variable blocks (from 700) with much more readable code! "Kludge" or not it definitely works!! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-22 by @gfwilliams The |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-10-31 by Moray Is this still best practice for maximising memory efficiency? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-11-02 by @gfwilliams Yes and no... I'd:
The Web IDE also lets you minify code that you're written in the right-hand pane, which should help a lot (although it makes it less debuggable) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-11-07 by Moray Relatedly, I still haven't tracked my memory leaks down, so I am using this pattern for those devices (sensors) which only poll occasionally and do not require realtime:
Okay, I should find and fix my code leaks, but this is a common and/or reasonable pattern in embedded? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-11-07 by @gfwilliams Well, it seems a bit hacky - but yes - I don't see anything wrong with that at all. Another thing you could do if you really want to 'hard' reset the chip is : The ... what I should do at some point is have a |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-11-08 by DrAzzy Correct practice is to find and fix the bugs - reloading the script every time it runs is a really painful solution, though I guess it is a solution... I've had Espruinii running for over a month without them dying, so it is possible. Just gotta find out where the memory is going. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-11-08 by @allObjects In your code, do you require modules? They may hold on to some memory... The other 'wild goose chase' is: do you 'write' to something that buffers but cannot empty the buffer? I do not know if this is still true when writing to console in (IDE) disconnected case, a buffer will fill up and Espruino will eventually freeze... but you did not mention freeze, but memory loss. Can you share some of the memory-destroying-suspicious code? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-11-09 by @gfwilliams
That doesn't happen if you're disconnected from USB, so you're ok... The best thing to do is to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2014-11-21 by DaveNI
I have been really pleased with how much code I can load/run within 48K ram. My current project is getting quite large yet when using minification I still have room for 700 variables.
I would still like to ensure I use as little memory as possible (just in case I hit the limit).
Minification works really well but for those variables that the minifier cannot rename i.e. those that interface with the object (at root level) what length should I try to keep variable names to?
i.e. what are "Short" variable names - I saw this mentioned somewhere but can't find it again?
If I have a module defining an object/class of which there will only ever be one instance, is there any benefit (or overhead!) in having all functions against the objects prototype?
I also tried placing all my main code within onInit(). i.e. instead of:
I used:
This allowed the minifier to rename most of my variables/functions but resulted in more memory being used - why did this happen?
I have read http://www.espruino.com/Internals but not 100% sure about the best approach.
Any other ideas on how to minimise memory usage yet keep my code readable/maintainable would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions