[Microbit-Python] memory error

Damien George damien.p.george at gmail.com
Fri Oct 2 23:46:23 CEST 2015


Yes, variable name length will have a small impact on RAM usage.

Yes, as David says, a given variable is only stored once in RAM and
then referenced by an index (which is usually 16 bits) so it's
relatively efficient.  You need to keep the actual name of the
variable around so that you can given sensible error messages, and
also for lookup of global names in dictionaries.

There are definitely things that we can do to improve the memory usage
and allow bigger scripts.  Do you think it's a priority?

On Fri, Oct 2, 2015 at 6:17 PM, David Whale <david at thinkingbinaries.com> wrote:
> I only had a 1K ZX81 and it forced me to learn machine code so I could make
> everything fit :)
>
> I'm *guessing* that MicroPython has a fixup table, so that the variable
> names and function names are only stored in RAM once - i.e. each reference
> to a function name or variable name in the bytecode is actually an index
> into a string table with the actual name in it? Perhaps a one-way hash could
> be used to turn any given name into a shorter hash that is actually used for
> the lookup?
>
> Cheers
>
> D
>
>
> ___________________________________________________________
> David Whale, B.Sc (Hons), MIET
> Software Engineer and IET Schools Liaison Officer, Essex
>
> email:  dwhale at theiet.org
> twitter: @whaleygeek
> blog:  blog.whaleygeek.co.uk
>
> Co-author of the new book "Adventures in Minecraft" - lets get kids coding!
>
>
> On 2 October 2015 at 18:06, Alan <alainjackson at hotmail.com> wrote:
>>
>> Hi,
>>
>> Am I right in thinking the size of variable names also makes a difference?
>> It seemed to, although I didn't test that scientifically.
>>
>> 3KB is a bit small. I know, however big the memory we'd always want more.
>> And I did grow up programming a ZX81, so this is 3 times as big! You'd think
>> I'd be happy with that.
>>
>> Cheers,
>>
>> Alan
>>
>> > Date: Fri, 2 Oct 2015 16:07:02 +0100
>>
>> > From: damien.p.george at gmail.com
>> > To: microbit at python.org
>> > Subject: Re: [Microbit-Python] memory error
>> >
>> > Hi Alan,
>> >
>> > Yes, doc strings do take up memory, so in our case (when trying to
>> > reduce memory usage) it's better to make them comments (which are just
>> > skipped over by the tokeniser).
>> >
>> > The example you give with lots of prints does indeed take up all the
>> > memory, so that's why it gives a MemoryError. It's not easy to fix
>> > this, the microbit is just really low on RAM! We could compile the
>> > python script to flash (instead of RAM) but that needs a bit of effort
>> > to get working.
>> >
>> > Cheers,
>> > Damien.
>> >
>> >
>> > On Thu, Oct 1, 2015 at 2:52 AM, Alan <alainjackson at hotmail.com> wrote:
>> > > Hi,
>> > >
>> > > Another little trick I've realised is that if I put a large,
>> > > explanatory
>> > > comment at the top of the file using """...""" it of course goes into
>> > > a
>> > > docstring... which takes up memory!
>> > >
>> > > If I use # style comments instead, it doesn't seem to take up memory.
>> > >
>> > > Cheers,
>> > >
>> > > Alan
>> > >
>> > > ________________________________
>> > > From: alainjackson at hotmail.com
>> > > To: microbit at python.org
>> > > Date: Thu, 1 Oct 2015 00:32:14 +0000
>> > >
>> > > Subject: Re: [Microbit-Python] memory error
>> > >
>> > > Hi,
>> > >
>> > > I didn't save the version of my code that got the memory error.
>> > >
>> > > But I've just now created a small, pathological example.
>> > >
>> > > The code below, which is 3kB on the disk, will create the following
>> > > error:
>> > >
>> > >>>> MemoryError:
>> > > Micro Python v1.4.5-68-g3a2171e on 2015-09-04; micro:bit with nRF51822
>> > > Type "help()" for more information.
>> > >
>> > > But if you delete the last line, it will run fine.
>> > >
>> > >
>> > >
>> > >
>> > > """
>> > > Testing how much memory for python code the microbit has.
>> > > """
>> > >
>> > > from microbit import *
>> > >
>> > >
>> > >
>> > >
>> > >
>> > > display.print("123456789022345678903234567890423456789052345678906234567890723456789082345678909234567890")
>> > >
>> > > display.print("a12345678902234567890323456789042345678905234567890623456789072345678908234567890923456789")
>> > >
>> > > display.print("ba1234567890223456789032345678904234567890523456789062345678907234567890823456789092345678")
>> > >
>> > > display.print("cba123456789022345678903234567890423456789052345678906234567890723456789082345678909234567")
>> > >
>> > > display.print("dcba12345678902234567890323456789042345678905234567890623456789072345678908234567890923456")
>> > >
>> > > display.print("ecba12345678902234567890323456789042345678905234567890623456789072345678908234567890923456")
>> > >
>> > > display.print("fecba1234567890223456789032345678904234567890523456789062345678907234567890823456789092345")
>> > >
>> > > display.print("gfecba123456789022345678903234567890423456789052345678906234567890723456789082345678909234")
>> > >
>> > > display.print("hgfecba12345678902234567890323456789042345678905234567890623456789072345678908234567890923")
>> > >
>> > > display.print("ihgfecba1234567890223456789032345678904234567890523456789062345678907234567890823456789092")
>> > >
>> > > display.print("123456789022345678903234567890423456789052345678906234567890723456789082345678909234567890")
>> > >
>> > > display.print("a12345678902234567890323456789042345678905234567890623456789072345678908234567890923456789")
>> > >
>> > > display.print("ba1234567890223456789032345678904234567890523456789062345678907234567890823456789092345678")
>> > >
>> > > display.print("cba123456789022345678903234567890423456789052345678906234567890723456789082345678909234567")
>> > >
>> > > display.print("dcba12345678902234567890323456789042345678905234567890623456789072345678908234567890923456")
>> > >
>> > > display.print("ecba12345678902234567890323456789042345678905234567890623456789072345678908234567890923456")
>> > >
>> > > display.print("fecba1234567890223456789032345678904234567890523456789062345678907234567890823456789092345")
>> > >
>> > > display.print("gfecba123456789022345678903234567890423456789052345678906234567890723456789082345678909234")
>> > >
>> > > display.print("hgfecba12345678902234567890323456789042345678905234567890623456789072345678908234567890923")
>> > >
>> > > display.print("ihgfecba1234567890223456789032345678904234567890523456789062345678907234567890823456789092")
>> > >
>> > > display.print("123456789022345678903234567890423456789052345678906234567890723456789082345678909234567890")
>> > >
>> > > display.print("a12345678902234567890323456789042345678905234567890623456789072345678908234567890923456789")
>> > >
>> > > display.print("ba1234567890223456789032345678904234567890523456789062345678907234567890823456789092345678")
>> > >
>> > > display.print("cba123456789022345678903234567890423456789052345678906234567890723456789082345678909234567")
>> > >
>> > > display.print("dcba12345678902234567890323456789042345678905234567890623456789072345678908234567890923456")
>> > >
>> > > display.print("ecba12345678902234567890323456789042345678905234567890623456789072345678908234567890923456")
>> > >
>> > > display.print("fecba1234567890223456789032345678904234567890523456789062345678907234567890823456789092345")
>> > >
>> > >
>> > >
>> > >
>> > >
>> > >
>> > >
>> > >> Date: Wed, 30 Sep 2015 12:01:57 +0100
>> > >> From: damien.p.george at gmail.com
>> > >> To: microbit at python.org
>> > >> Subject: Re: [Microbit-Python] memory error
>> > >>
>> > >> Hi Alan,
>> > >>
>> > >> Can you post your code?
>> > >>
>> > >> On Wed, Sep 30, 2015 at 2:14 AM, Alan <alainjackson at hotmail.com>
>> > >> wrote:
>> > >> > Hi,
>> > >> >
>> > >> > How much memory does the Microbit have for a python program?
>> > >> >
>> > >> > I've written a small program about 105 lines long. It's 3kB on my
>> > >> > hard
>> > >> > disk.
>> > >> > It runs ok now but when it was 115 lines long and 3.2kB I kept
>> > >> > getting
>> > >> > memory allocation errors as soon as I pressed the reset button. If
>> > >> > I
>> > >> > commented about 10 lines out of my program it would run and it
>> > >> > didn't
>> > >> > seem
>> > >> > to matter which 10 lines. So I assumed it was out of program
>> > >> > memory?
>> > >> >
>> > >> > Cheers,
>> > >> >
>> > >> > Alan
>> > >> >
>> > >> > _______________________________________________
>> > >> > Microbit mailing list
>> > >> > Microbit at python.org
>> > >> > https://mail.python.org/mailman/listinfo/microbit
>> > >> >
>> > >> _______________________________________________
>> > >> Microbit mailing list
>> > >> Microbit at python.org
>> > >> https://mail.python.org/mailman/listinfo/microbit
>> > >
>> > > _______________________________________________ Microbit mailing list
>> > > Microbit at python.org https://mail.python.org/mailman/listinfo/microbit
>> > >
>> > > _______________________________________________
>> > > Microbit mailing list
>> > > Microbit at python.org
>> > > https://mail.python.org/mailman/listinfo/microbit
>> > >
>> > _______________________________________________
>> > Microbit mailing list
>> > Microbit at python.org
>> > https://mail.python.org/mailman/listinfo/microbit
>>
>> _______________________________________________
>> Microbit mailing list
>> Microbit at python.org
>> https://mail.python.org/mailman/listinfo/microbit
>>
>
>
> _______________________________________________
> Microbit mailing list
> Microbit at python.org
> https://mail.python.org/mailman/listinfo/microbit
>


More information about the Microbit mailing list