free ram available

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

I am teaching myself AVR and C for a while, now I came across a question:

How can I find out how much free ram is available for my code??

if someone can explain to me as simple as possible will be great(as I am still a newbie), or point me to a tutorial.

Thanks

Zhuhua Wu - Electronic Engineering Student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Code runs from flash rom. Ram is used for stack and variables. This is described in the AVR datasheet.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you're using AVR GCC, it (and by extension, Atmel Studio) will give you a report on program (flash) and data (RAM) utilization when you build a project. You'll see something like this in the build output:

		AVR Memory Usage
		----------------
		Device: atmega644p
		Program:   10868 bytes (16.6% Full)
		(.text + .data + .bootloader)
		Data:        326 bytes (8.0% Full)
		(.data + .bss + .noinit)
	Done executing task "RunCompilerTask".
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I recall correctly, the memory usage report gives you the number of assigned bytes in SRAM. It DOES NOT include stack, either of the call/return variety or of the function parameter type.

Many programmers use some allowance like 2/3 or 3/4 of total RAM for the Data section. The remainder is for stack allowance.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
the memory usage report gives you the number of assigned bytes in SRAM. It DOES NOT include stack, either of the call/return variety or of the function parameter type.

Don't forget the local variables. They take up more space than the globals in many programs.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

in atmel studio 6, here is what I got, but it doesn't look right to me:

				Program Memory Usage 	:	458 bytes   11.2 % Full
				Data Memory Usage 		:	0 bytes   0.0 % Full

and here is some code I found on a arduino book written by Briann Evans:

int freeRAM()
{
	extern int __heap_start, *__brkval;
	int v;
	return (int) &v - (__brkval == 0 ? (int) &__heap_start: (int) __brkval;
}

I don't have a clue what it means, can it be translate to AVR C?

Zhuhua Wu - Electronic Engineering Student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
It DOES NOT include stack

Is that true?, most setup I know of set a size for the stack, so the program still work when 100% data is used.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not in GCC, the .data and .bss variables are allocated from the start of SRAM and the single SP is initialised to RAM end. It works its way down as you nest function calls and allocate automatics until it bumps into the end of .bss (or the heap if it's being used). There is no protection, it's up to the programmer to understand what's going on and ensure it simply does not happen.

The freeram() function presented above is GCC and should work unmodified. It simply relies on the fact the heap_start is at end of .bss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is that true?

Yes. The usual scheme is to let the stack and the heap share the same memory area, starting at opposite ends and grow towards each other. In essence, the memory report tells you how much RAM there is available for this.

http://www.nongnu.org/avr-libc/u...

What the memory report does not tell you is the maximum stack space consumed during run time. This, while possibly doable, is hard to determine. Thus, people tend to user rules of thumb. With experience these can be quite good.

So, depending on where you choose to stand in the "local/global variables debate", how much locals you have, how much recursive function calls you dare do, and the nature of your malloc() calls (if any) the rule of thumb needs to be tweaked and "personalized".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I don't have a clue what it means, can it be translate to AVR C?
It is AVR C, so no translation necessary. Some explanation is in order, however. __heap_start and __brkval are values kept by avr-libc that indicate the beginning of the heap space and the end of the heap space. The heap is the section in memory that is highest in the memory space (aside from the stack). if __brkval is equal to 0, then nothing was ever allocated on the heap, so you can use __heap_start as the highest used memory space, otherwise __brkval is used. The function then uses the trick of defining a local variable. The address of that local variable is the lowest current used memory space of the stack. Subtracting the highest used memory value (either __heap_start or __brkval from above) will give you the amount of unused memory space in between. If that number is negative, then obviously your stack has clobbered at least some of your data.

Regards,
Steve A.

The Board helps those that help themselves.