Go Down

Topic: Why the sketch size and amount of RAM used by DUE is more than MEGA. (Read 4251 times) previous topic - next topic

sohini25

Hi,

Can anybody please tell me why due sketches occupy more RAM compared to Mega for the same code. I need to make an analysis.

Please help to solve my query.

wallaceb

i am taking a wild guess, but it could easily be the difference in the processor. What i mean by that is your code in the sketch is the same, but the underlying library code is different between the MEGA and Due as a result of the difference in CPU architecture. i would imagine that if the libraries were analyzed, they are where the extra memory is being used.

sohini25

Can you elaborate a little more about the type of difference in microprocessor architecture?

wallaceb


Can you elaborate a little more about the type of difference in microprocessor architecture?


this is still just guessing, but it is what i believe is causing the difference.

the MEGA uses an 8-bit ATMEL AVR processor, while the DUE uses a 32-bit ATMEL ARM processor. the differences between the AVR and ARM processors would likely cause changes in memory usage. for example, within the AVR processor, you can use PROGMEM commands to store strings in flash and not in RAM, while the ARM processor does not support that, changing the way strings are loaded into RAM.

along the lines of the processor differences, the actual line-by-line code seen in assembly language to make the MEGA perform command XXX and the line-by-line code to make the DUE perform the same command would be different. the reasons for the difference is that the memory addresses are different ect.. With the ARM processor being 32-bit, it can handle larger calculations within smaller code cycles as the 8-bit processor would have to perform more line commands within memory to complete the calculation.

because of these and other differences i could easily see the same library compile quite differently depending on which processor is chosen.

unfortunately this is only a guess on my part. hopefully someone else may be able to supply a better answer.

AdderD

There are a variety of reasons. One is that the MEGA uses an 8-bit processor while the Due is 32 bit. Consider the following line:

int someVariable;

How much space does that take up? Usually ints are the size of the processor or 8 bits for the Mega and 32 bits for the Due. However, I think that ints on the mega are actually 16 bit. Still, the exact line of code is at least twice the amount of RAM on the Due as it is on the Mega. This will cause identical code to take up different amounts of RAM depending on which processor you compile it for.

This same thing holds true for other things. For instance, function calls need to place memory addresses on the stack. Memory addresses sizes differ between processors so you will have RAM usage differences.


Palliser

Give their different MCU's, Due (32-bit Cortex-M3 based SAM3X8E) and Mega ( 8-bit AVR RISC ATmega2560), they use a different version of the same programming GNU tool chain (compiler, liker, additional utilities). AVR for Mega and ARM-none-eabi for Due, which means that the coding is also different. 32-bit vs 8-bit program instructions and memory locations means also different coding sizes. Regards! 

graynomad

As we're talking about RAM I think it's totally due to the difference in variable sizes, eg ints and pointers are 16 bits on a Mega, 32 on a Due.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

greghol

Actually int is 16.

Due:
Size of int: 4, Size of uint: 4
Size of char: 1, Size of uchar: 1
Size of float: 4, Size of double: 8
Size of long: 4, Size of longlong: 8

graynomad

Quote
Size of int: 4

4x8=?

Can you try a pointer as well?

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

greghol


Quote
4x8=?

Can you try a pointer as well?


My bad, Int is 4 bytes, short is 2 bytes.
Pointer appears to be 4 bytes.

Code below. Adapted from another post on memory usage.

-Greg

Dynamic ram used: 0
Program static ram used 2616
Stack ram used 72
My guess at free mem: 95616

Size of int: 4, Size of uint: 4
Size of char: 1, Size of uchar: 1
Size of short: 2, Size of ushort: 2
Size of float: 4, Size of double: 8
Size of long: 4, Size of longlong: 8
Size of pointer (ptr_i): 4
Addr of pointer (ptr_i): 0x20087fe4
Value of i: 0xaa

Code: [Select]

#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

extern char _end;
extern "C" char *sbrk(int i);
char *ramstart=(char *)0x20070000;
char *ramend=(char *)0x20088000;

void setup()
{
   Serial.begin(9600);
   while(!Serial);
   
   char *heapend=sbrk(0);
   register char * stack_ptr asm ("sp");
   struct mallinfo mi=mallinfo();
   printf("\nDynamic ram used: %d\n",mi.uordblks);
   printf("Program static ram used %d\n",&_end - ramstart);
   printf("Stack ram used %d\n",ramend - stack_ptr);
   printf("My guess at free mem: %d\n\n",stack_ptr - heapend + mi.fordblks);
   
   int* ptr_i;
   int i = 0xaa;
   ptr_i = &i;
   
   printf("Size of int: %d, Size of uint: %d\n", sizeof(int), sizeof(unsigned int));
   printf("Size of char: %d, Size of uchar: %d\n", sizeof(char), sizeof(unsigned char));
   printf("Size of short: %d, Size of ushort: %d\n", sizeof(short), sizeof(unsigned short));
   printf("Size of float: %d, Size of double: %d\n", sizeof(float), sizeof(double));
   printf("Size of long: %d, Size of longlong: %d\n", sizeof(long), sizeof(long long));
   printf("Size of pointer (ptr_i): %d\n", sizeof(ptr_i));
   printf("Addr of pointer (ptr_i): 0x%x\n", ptr_i);
   printf("Value of i: 0x%x\n", *ptr_i);
}

void loop() {
  // put your main code here, to run repeatedly:
 
}

Go Up