Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HEAP memory

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Advanced Member level 3
Joined
Apr 24, 2010
Messages
890
Helped
24
Reputation
48
Reaction score
25
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Activity points
8,177
In a microcontroller, there is a HEAP memory. I was looking for a good article on this. What is this, when to use it, how to use it, why need to use it in detail. There are no such documents online except only some summaries. Does anyone have any good document or link or any detailed information about this HEAP momory?

Thanks in Advance.
 

KlausST

Super Moderator
Staff member
Advanced Member level 7
Joined
Apr 17, 2014
Messages
23,048
Helped
4,716
Reputation
9,448
Reaction score
5,092
Trophy points
1,393
Activity points
152,683
Hi,

I may be mistaken, but I've never heard about a microcontroller (hardware) to have heap memory.

I rather think "heap memory" is some kind of RAM area allocated/provided by a compiler.

Thus I'd rather read a compiler document than a microcontroller document.

Klaus
 

FvM

Super Moderator
Staff member
Advanced Member level 7
Joined
Jan 22, 2008
Messages
50,813
Helped
14,603
Reputation
29,481
Reaction score
13,688
Trophy points
1,393
Location
Bochum, Germany
Activity points
290,949
Heap is the area used for dynamic memory allocation, either explicitely done with malloc() or implicitely by libraries using it internally, e.g. for file I/O. Simple embedded applications can probably disable it. As said, you get all neccessary information in the compiler and library docs.
 

KlausST

Super Moderator
Staff member
Advanced Member level 7
Joined
Apr 17, 2014
Messages
23,048
Helped
4,716
Reputation
9,448
Reaction score
5,092
Trophy points
1,393
Activity points
152,683
Hi,

I don´t know what compiler you are using.
So the best I can do ist to recommend to read through your compiler´s documentation.
I can´t give a guarantee that it is well documented ... since obviously I don´t know your compiler´s documentation.

I don´t know what it depends on. Maybe the compiler, maybe the comiler setup, maybe the microcontroller.
From what I think I know: It´s not stack, it´s not fixed variables, it´s all the "temporary" variables. But all three may be located in the RAM of a microcontroller.

Klaus
 

Mithun_K_Das

Advanced Member level 3
Advanced Member level 3
Joined
Apr 24, 2010
Messages
890
Helped
24
Reputation
48
Reaction score
25
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Activity points
8,177
I'm using MikroC pro for PIC. There is no such explanation about heap. But there is always option in the properties to set memory as heap. I want to know what is this heap and why we need to use it and when we should uese it?
 

FvM

Super Moderator
Staff member
Advanced Member level 7
Joined
Jan 22, 2008
Messages
50,813
Helped
14,603
Reputation
29,481
Reaction score
13,688
Trophy points
1,393
Location
Bochum, Germany
Activity points
290,949
Don't hold us responsible for the poor MikroC documentation. As far as I see, only the PIC32 MikroC version actually makes use of dynamic memory. For the 8 and 16 Bit compilers, heap storage class is mentioned once in the manual, don't think that it has a real function. Just ignore it.

For Microchip compilers, dynamic memory is also available with XC16.
 

Aussie Susan

Advanced Member level 4
Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,474
Helped
402
Reputation
804
Reaction score
440
Trophy points
1,363
Activity points
17,322
While heap memory is used for dynamic memory allocation (as stated above), be VERY careful about the implementation of those functions.
In a PC of other environment when applications finish and can be started again by the OS, an embedded system is designed to work 'for ever' (i.e. they should never return from the 'main()' function).
If the dynamic memory allocation and return algorithms are 'simplistic', then you tend to get fragmentation unless you are VERY careful with how you allocate and free memory. On the other hand, if you use some of the more sophisticated algorithms that minimise fragmentation (not 'minimise, not necessarily 'eliminate') then they will take up more space (and runtime) and still work best when you use then carefully.
The underlying problem is that, if you run out of available heap space (generally due to fragmentation) then either your app stops because you trap the lack of memory condition, or (worst case) you don't realise and treat the (normally) NULL pointer as valid which can cause all sorts of problems.
My golden rule is embedded systems should avoid dynamic memory allocations, and if there REALLY is no other way (I've only ever come across 1 situation) then you must fully understand the algorithms involved and call the calloc/malloc/free functions in such a way that the heap memory is returned in the order it is allocated (or as appropriate for the algorithm) so that it never becomes fragmented.
Susan
 

paulfjujo

Advanced Member level 4
Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,459
Helped
296
Reputation
592
Reaction score
279
Trophy points
1,363
Location
France 01800
Activity points
10,409
I'm using MikroC pro for PIC. There is no such explanation about heap. But there is always option in the properties to set memory as heap. I want to know what is this heap and why we need to use it and when we should uese it?

hello,

i allready use Heap memory to store some Fonts charactere to use with a LCD



piece of code

Code:
UART1_Write_CText(" Init Memory Manager \r\n");
  MM_Init();
  //This function is used to determine total free memory size.
  L1=MM_TotalFreeMemSize();
  LongWordToStr(L1,CRam1);
  UART1_Write_CText(" Total RAM Free = ");
  UART1_Write_Text(CRam1); CRLF1();
  // largest available free memory block for the Heap.
  L1=MM_LargestFreeMemBlock();
  LongWordToStr(L1,CRam1);
  UART1_Write_CText(" Taille Maxi Free Bloc memoire = ");
  UART1_Write_Text(CRam1); CRLF1();
..

in the mikroC Project
i defined this heap area ..
wich can be used many times, by using pointer ... and mem alloc functions
This zone decrease the global amount of RAM !

example to select font

Code:
void Select_Font_Type(int Type)
{
   Print_Cte_String(" Taille Font ");
   switch(Type)
   {
    case 1:
      {
      Print_Cte_String(" TerminaL6x8 = " );
      km=sizeof(TerminaL6x8) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
      for (i=0;i<km;i++)  *(pp+i)=TerminaL6x8[i];
      Print_Cte_String(" Set Font.\r\n");
      SetFont(pp);
      Print_Cte_String(" Param Font :\r\n");
      Param_Font();
      break;
      }
     case 2:
      {
       Print_Cte_String(" TerminaL12x16 = ");
       km=sizeof(TerminaL12x16) ;
       WordToStr(km,CRam1);
       UART1_Write_Text(CRam1); CRLF1();
       pp= (unsigned char *) Malloc(km);
       if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
       for (i=0;i<km;i++)  *(pp+i)=TerminaL12x16[i];
       SetFont(pp);
       Param_Font();
       break ;
      }
     case 3:
      {
       Print_Cte_String(" Trebuchet_MS13x21= ");
      km=sizeof(Trebuchet_MS13x21) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
      for (i=0;i<km;i++)  *(pp+i)=Trebuchet_MS13x21[i];
      SetFont(pp);
      Param_Font();
      break;
      }
      
      case 4:
      {
      // Print_Cte_String("Comic_Sans_MS20x20 = "); // taille TROP GRANDE >5500 bytes
      Print_Cte_String(" Terminal5x11 =");     // taille 1062
      km=sizeof(Terminal5x11) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
       for (i=0;i<km;i++)  *(pp+i)=Terminal5x11[i];
      SetFont(pp);
      Param_Font();
      break;
      }

     case 5:
      {
       Print_Cte_String(" Trebuchet_MS16x23= ");
      km=sizeof(Trebuchet_MS16x23) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
       for (i=0;i<km;i++)  *(pp+i)=Trebuchet_MS16x23[i];
      SetFont(pp);
      Param_Font();
      break;
      }
      case 6:
      {
       Print_Cte_String(" Arial_Narrow22x32 = ");
      km=sizeof(Arial_Narrow22x32) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
      for (i=0;i<km;i++)  *(pp+i)=Arial_Narrow22x32[i];
      SetFont(pp);
      Param_Font();
      break;
      }
      case 7:
      {
      Print_Cte_String(" NI7SEG22x28 = ");
      km=sizeof( NI7SEG22x28) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      if (pp==0)
       {
          UART1_Write_CText(Mesg_Alerte);
          break;
       }
       for (i=0;i<km;i++)  *(pp+i)= NI7SEG22x28[i];
      SetFont(pp);
      Param_Font();
      break;
      }

     case 8:
      {
      Print_Cte_String(" NI7SEG26x35 =");
      km=sizeof(NI7SEG26x35 ) ;
      WordToStr(km,CRam1);
      UART1_Write_Text(CRam1); CRLF1();
      pp= (unsigned char *) Malloc(km);
      for (i=0;i<km;i++)  *(pp+i)= NI7SEG26x35[i];
      SetFont(pp);
      Param_Font();
      break;
      }
      defautlt:
      UART1_Write_CText("..Erreur !!! \r\n");
      Delay_1sec();
      break;
    }
  }
 

betwixt

Super Moderator
Staff member
Advanced Member level 7
Joined
Jul 4, 2009
Messages
15,731
Helped
5,069
Reputation
10,163
Reaction score
4,924
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
133,222
I think there is a misunderstanding of what 'Heap' memory actually is.
Heap is the same RAM as is used to hold variables and operating system work space, its a partition of the same memory rather than a different memory.

When your compiler produces an executable program it allocates a region of RAM to hold the named variables and a region to hold the stack. It may also reserve addresses to hold constants declared within the program although better compilers will store fixed values (constants) in program space instead.

What is left over when all the known required addresses have been allocated is called the heap. It is still physical memory but not directly attributed to a variable, it can be used for other purposes. The compiler usually adds some code that dynamically uses then frees the memory as needed in order for functions to work but it also offers you the opportunity to reserve memory for your own use through the malloc() (memory allocator) and calloc() functions. Typically this is where you would store tables or arrays of data when the size is calculated by the program as it runs and therefore can't be reserved at compilation time. Normally, you would release the memory when you have finished with it by using the free() function, if you don't there is a risk of running out of memory or it becoming corrupted. Imagine using malloc() in a loop for example where each pass grabs another block of memory from the heap.

Heap memory is no faster than any other part of memory so there is little advantage to using it as a cache or font store, it may even be slower as access might have to pass through the compilers memory management process. If your font is fixed, it would be more advantageous to save the font as 'const' so it becomes part of the program code, leaving more RAM free for other things.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top