How to see available RAM usage?

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dparson
 
Posts: 7
Joined: Sat Jan 18, 2014 4:55 am

How to see available RAM usage?

Post by dparson »

How do you see the available RAM using CircuitPython? In the api reference http://circuitpython.readthedocs.io/en/ ... nit__.html there are some commands to show information about memory but I get an error trying to import the module.

Code: Select all

import ustack
print(ustack.max_stack_usage())
According to Memory Usage on https://learn.adafruit.com/porting-an-a ... or/gotchas CircuitPython on the SAMD21 runs out of memory after 200-300 lines, so I want to see how RAM is being used as I create my project.

Thank you!

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: How to see available RAM usage?

Post by kattni »

I'm not sure why ustack isn't working for you. However, I have another suggestion. Try the following:

Code: Select all

import gc
gc.mem_free()
This will give you the available remaining heap RAM. You can also include:

Code: Select all

gc.collect()
This will run the garbage collector, which otherwise runs irregularly. So, without running collect first, you might appear to have less left than you really do.

Hope this helps!
~Kattni

User avatar
dparson
 
Posts: 7
Joined: Sat Jan 18, 2014 4:55 am

Re: How to see available RAM usage?

Post by dparson »

Thanks @kattni, that did the trick!

Do you know where the source of truth is for what modules are available? gc isn't listed on http://circuitpython.readthedocs.io/en/ ... index.html but it imports just fine, while ustack is listed but does not exist.

Code: Select all

Adafruit CircuitPython 2.1.0 on 2017-10-17; Adafruit Metro M0 Express with samd21g18
>>> import ustack
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'ustack'
>>> import gc
>>> gc.collect()
>>> gc.mem_free()
19680
So it turns out gc.mem_free() isn't showing me memory usage in the way I was hoping but that is ok, it just demonstrates that I have a lot to learn about what is happening behind the scenes :) I tried a dir(gc) and saw there was a mem_alloc function, and that seems to show an increasing value as a script executes.

For anyone interested, attached is a file to show a memory allocation failure. It has many print statements of arbitrary length and with 125 of them, it runs ok. If you add 1 more it will fail with the memory error.

With 125 print statements:

Code: Select all

...
testing the memory usage
testing the memory usage
testing the memory usage
Free Memory at Start: 11664
Free Memory at End  : 11568
Memory Allocated at Start: 8320
Memory Allocated at End  : 8560
With 126 print statements:

Code: Select all

code.py output:
MemoryError: memory allocation failed, allocating 1282 bytes
Attachments
code.py.txt
(4.77 KiB) Downloaded 190 times

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: How to see available RAM usage?

Post by kattni »

I'm glad this helped!

We're incredibly tight on space with CircuitPython, and the decision was made to not include ustack. Once the M4 boards are released, there will be less concern for space, and there may be consideration for reenabling ustack.

There isn't another list of modules. ReadTheDocs gets into more detail on some things and less on others than we would like. There is a lot of carryover from MicroPython and it hasn't been really decided how we're going to proceed. RTD has a lot a valuable information, but it can also be confusing at times.

If you continue to run into issues, I suggest posting more questions here or join us on the Adafruit Discord #circuitpython channel.
~Kattni

User avatar
dparson
 
Posts: 7
Joined: Sat Jan 18, 2014 4:55 am

Re: How to see available RAM usage?

Post by dparson »

Awesome and thanks again!

User avatar
Dao20
 
Posts: 67
Joined: Wed Apr 16, 2014 8:14 pm

Re: How to see available RAM usage?

Post by Dao20 »

Hay @kattni. can you help with problems getting onto Discord? I obviously have a discord account (Dao20), only Discord wont let me use it on any other computer but the one(which isn't working now) I originally made the account with. I try going on Discord on my other computer which is only feet away from the other, but I can only view stuff. It wants me to 'claim' my own account, I put in the email & password, and then it says that my email is already registered. It's registered with my Discord Dao20 account.

User avatar
bogdanc
 
Posts: 6
Joined: Thu Nov 23, 2017 3:37 am

Re: How to see available RAM usage?

Post by bogdanc »

Hi Kattni,

What is the M4 board that you mention? Is it a new version of Circuit Playground Express? When will it be released and are there some specs available?

Thanks,
Bogdan

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”