How can I reduce the ram usage of a program?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

How can I reduce the ram usage of a program?

Post by Creative25 »

Hi
I just wrote a program that needs a little more ram than my chip has.
Memory Usage Report
===================
Error: No remaining RAM block (on target) big enough for:
'$ret' size:4 bytes

RAM available:128 bytes, used:131 bytes (102.3%), free:-3 bytes (-2.3%),
Heap size:0 bytes, Heap max single alloc:0 bytes
ROM available:2048 words, used:0 words (0.0%), free:2048 words (100.0%)



failure
Return code = -2
Flowcode was unable to assemble the ASM file due to the following errors:



FINISHED

I am sure it can be improved to use less ram.
What are the things that use lots of ram?
How can I reduce the usage?
Best regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: How can I reduce the ram usage of a program?

Post by Enamul »

Hi,
obviously if you are careful in declaring the variable like if you don't need variable value higher than 255. Declare then as byte not as integer.same is true for other data type. Other trick is too use macro and use local variable rather than using all global variable as local variables are reused by the compiler which reduces the requirements using lots of global variable. You can post your program so that I can show you how you can effectively reduce variable number.
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: How can I reduce the ram usage of a program?

Post by Creative25 »

Thanks.
I think I will be able to reduce a lot.
By using Macros with local variables.
Best Regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: How can I reduce the ram usage of a program?

Post by Enamul »

That's great! Please let us know if you need any help.
Enamul
University of Nottingham
enamul4mm@gmail.com

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: How can I reduce the ram usage of a program?

Post by Creative25 »

Hi Enamul.
I was ableto reduce the ram usage quite a lot, and i was able to compile it.
I just want get a better understanding of Local variables.

Are they slower than global variables, would you recommend them in a fast interrupt that is used for a software PWM, or is it better to use a global variable for this purpose?

Can I mix local and global variables in the same macro?

Do I always have to use a return function when using local variables, or can I simply put the end result of a macro into a global variable?

Best Regards:
Uli

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: How can I reduce the ram usage of a program?

Post by Enamul »

I was ableto reduce the ram usage quite a lot, and i was able to compile it.
Great..well done :)
Are they slower than global variables, would you recommend them in a fast interrupt that is used for a software PWM, or is it better to use a global variable for this purpose?
No, it has no link with access speed. Local variables are defined by compiler out of ram locations and re-used in different macro. so they are same as global variable but as you are not defining their location rather compiler is defining their address. so speed is same. so for fast PWM it doesn't make any difference.
Can I mix local and global variables in the same macro?
Of course you can..in fact we always do that.
Do I always have to use a return function when using local variables, or can I simply put the end result of a macro into a global variable?
if you are returning any thing from macro it's good to return using local return variable as that saves one variable declaration. You can put the end result in global variable but in that case it can be accessible from any macro just not the macro from which you are calling the macro.

Actually global variable is usable from any macro and it doesn't confined to any macro..whereas local variable are confined to macro so you can use that within the macro not outside that macro where you have declared..you will not even see the local variable in other macros other than the macro where you declared. so that mean if you declare "test" local variable in you 10 macros separately they could be using same ram location by compiler so saves 9 location if you had used global variable.
Enamul
University of Nottingham
enamul4mm@gmail.com

Post Reply