Go Down

Topic: SRAM optimization question (Read 267 times) previous topic - next topic


Working on a serious project where SRAM will become an issue I'm sure. I need to know the following:

1) during the program execution, eventually the RAM gets eaten right?

2) when ram is gone, the device stops responding right?

3) https://learn.adafruit.com/memories-of-an-arduino/optimizing-sram here is a very good info on RAM optimization (and i also have done few and still working on to make it further optimized). But i need to  know, what to do if say the things are getting to worse, during running, how to recover that from disaster?? [like in windows, we have some RAM free software those de-fragments the RAM] (I know a reboot is a big solution, but without rebooting of course...)


Get a device with enough memory.

Not if you code it properly. RAM gets used and released again.
Behavior is undefined
No such thing; just write the code correctly. You can use the watchdog to reset the Arduino. If your code gets stuck in a loop and your code does not kick the watchdog (in that loop) the Arduino will eventually reset. It's not a reliable way to recover from memory issues though.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.



Does your code (or any libraries that you include) use dynamic memory allocation?

If not, then memory doesn't get "eaten up" over time.

If dynamic memory allocation is used, make sure you (and the libraries) properly free it when it's no longer needed. And, that you (and the libraries) have proper exception handling for when the allocation function reports that there is no memory available.


Does your code (or any libraries that you include) use dynamic memory allocation?

how can i know that? Sample perhaps???


Working on a serious project where SRAM will become an issue I'm sure.
Maybe you should start by explaining that statement. What makes you think "SRAM will become an issue"? You haven't even told us what board you're using or shown us any code. You may be trying to solve a problem that you don't have.

What proof do you have that you're experiencing a memory leak? If you were, then you need to examine the code.

Since you don't appear to know what dynamic memory allocation is, it's not likely that you're using it --- at least on purpose. Does you code use any variables (objects) of the C++ String class? If so, learn how to use c-strings (null terminated arrays of char) instead. There are lots of references for that.

Next, look in the source code for the libraries you're using. Do they create new objects using the 'new' keyword or allocate memory using the malloc() function? Do they use the C++ String class?


how can i know that? Sample perhaps???
You know by looking at the libraries you use and seeing if they use dynamic memory or not.  Or you can put it together and show someone who knows how to look at it and tell.  But what you can't do is just barely describe the project in the vaguest of terms and expect that anyone else would be able to tell from that. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Coding Badly

...or add code to query at runtime.

Go Up