Introduction to RTOS Part 4 - Memory Management | Digi-Key Electronics

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ค. 2024
  • Memory management is important in a multi-threaded environment, as threads (or tasks) can quickly consume all of the allocated memory, causing bizarre effects such as overwritten memory locations or random processor resets.
    The solution to the challenge in the video can be found here: www.digikey.com/en/maker/proj...
    In this video, we look at various ways to allocate memory to a task and how to monitor memory usage in FreeRTOS.
    Most C programs rely on 3 different types of memory. Static memory is set aside prior to program execution and used for things like static variables, constants, and global variables. Stack is allowed to grow dynamically and consists of local variables declared at the function level. Finally, heap may also grow dynamically and must be specifically allocated and deallocated by the programmer (e.g. using the malloc() and free() functions, respectively).
    Whenever we create a new task in FreeRTOS, we must assign it a set amount of stack memory to use out of the global, available heap. Note that each task also requires another section of heap for the Task Control Block (TCB), which stores various attributes about the task, such as its state and priority.
    We demonstrate ways to monitor the stack in each thread as well as the total amount of heap available to the system. We also show what happens when you overrun the stack or heap!
    Product Links:
    www.digikey.com/en/products/d...
    Related Videos:
    Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? - • Introduction to RTOS P... ​
    Introduction to RTOS Part 2 - Getting Started with FreeRTOS - • Introduction to RTOS P... ​
    Introduction to RTOS Part 3 - Task Scheduling - • Introduction to RTOS P... ​
    Introduction to RTOS Part 4 - Memory Management - • Introduction to RTOS P... ​
    Introduction to RTOS Part 5 - Queue - • Introduction to RTOS P... ​
    Introduction to RTOS Part 6 - Mutex - • Introduction to RTOS P... ​
    Introduction to RTOS Part 7 - • Introduction to RTOS P... ​
    Introduction to RTOS Part 8 - • Introduction to RTOS P...
    Introduction to RTOS Part 9 - • Introduction to RTOS P...
    Introduction to RTOS Part 10 - • Introduction to RTOS P...
    Introduction to RTOS Part 11 - • Introduction to RTOS P...
    Introduction to RTOS Part 12 - • Introduction to RTOS P...
    Related Project Links:
    www.digikey.com/en/maker/proj...
    Related Articles:
    www.digikey.com/en/maker/vide...
    Learn more:
    Maker.io - www.digikey.com/en/maker
    Digi-Key’s Blog - TheCircuit www.digikey.com/en/blog
    Connect with Digi-Key on Facebook / digikey.electronics
    And follow us on Twitter / digikey
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 71

  • @AlphaCrucis
    @AlphaCrucis ปีที่แล้ว +11

    "Good luck. We're all counting on you." I love this guy. He's good at explaining, makes it interesting, makes good use of visual aids and demos, and has a sense of humor. Thank you Shawn!

  • @Henrix1998
    @Henrix1998 ปีที่แล้ว +16

    This channel is basically covering one of my courses thats painfully unclear, absolutely awesome

    • @Apocobat
      @Apocobat ปีที่แล้ว +4

      real time embedded systems at my school, although lecture wise is ok, does not cover hardware implementation at all. Im really disappointed considering that my school is supposed to be one of the more advanced in terms of software/hardware education. It doesnt help that they pulled the lab ta off the street and he knows virtually nothing about rtos and wants to teach us about linux and computer os instead which is completely disconnected from the course content.

  • @adityasreeram.k.s9527
    @adityasreeram.k.s9527 4 หลายเดือนก่อน +5

    For those who are referring his solution form website, do note that instead of "idx ;" correct it as "idx++;" .If index in not incremented there can be chance for assert failed error.
    Hope this helps someone.

    • @claytube1958
      @claytube1958 3 หลายเดือนก่อน +2

      I found that too..

  • @turkey343434
    @turkey343434 3 ปีที่แล้ว +9

    Excellent content. Thank you Shawn, keep them coming!

  • @Astronomer6573
    @Astronomer6573 2 ปีที่แล้ว +3

    Really good explanation and visual demonstration on how memory allocation should be done. Your voice can really stay in my head, looking forward to all of your videos!

  • @minefacex
    @minefacex 2 ปีที่แล้ว +1

    This guy is seriously amazing! Thanks for the vid!

  • @achalasamapriya2701
    @achalasamapriya2701 3 ปีที่แล้ว +6

    was waiting for this.. thanks

  • @zee-lab-electronics
    @zee-lab-electronics 3 ปีที่แล้ว +1

    Awesome work Shawn

  • @Dygear
    @Dygear 3 ปีที่แล้ว +12

    Nice Mission Impossible reference! Your voice does remind me of the guy on the tape, now that you said it.

    • @ShawnHymel
      @ShawnHymel 3 ปีที่แล้ว +7

      I was hoping someone would catch that :)

  • @shubhamsen4572
    @shubhamsen4572 3 ปีที่แล้ว +1

    This guy is excellent

  • @perlmansoong944
    @perlmansoong944 ปีที่แล้ว

    Great video, Shawn! And I've a question. From 2:46, what is the usage of the light-orange-colored Stack (at the top of the RAM pattern)? Seems that this part is unused in FreeRTOS system, doesn't it? Thank you :-)

  • @MuhammadDaudkhanTV100
    @MuhammadDaudkhanTV100 3 ปีที่แล้ว +2

    Really good ideas

  • @baobuiquoc4763
    @baobuiquoc4763 3 ปีที่แล้ว +1

    Dear Mr Shawn, thanks for your video. On your solution code, you use a global variable msg_flag. Will it create a race condition between the printMessage task and the interrupt handler? i.e. before the task clear the flag, interrupt occur and set the flag, then return to the task and the task clear the flag. The message will be missed, and the msg_ptr is not freed

  • @maurod6180
    @maurod6180 2 ปีที่แล้ว

    thank you very much!!!!!

  • @Hasan...
    @Hasan... 3 ปีที่แล้ว +13

    Shawn, are you planning to upload a quick tutorial that uses both cores of ESP32 soon?

    • @ShawnHymel
      @ShawnHymel 3 ปีที่แล้ว +16

      I wasn't planning on it, as these videos are focused on RTOS concepts and FreeRTOS (the ESP32 just happened to be a really easy way to start using FreeRTOS). However, I'm glad to know there's interest in talking about multi-core operations and the ESP32, so I'll keep that in mind. For now, I recommend starting by looking the differences between ESP-IDF and FreeRTOS to get a sense of how the ESP32 uses its 2 cores when running tasks: docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/freertos-smp.html

  • @minefacex
    @minefacex 2 ปีที่แล้ว

    Hey Shawn! I have realized that if I put vTaskDelay(100 / portTICK_PERIOD_MS); at the and of the task where you demonstrated the stack free space size problem, there is no Guru Meditation Error! Why is that?

  • @jasonleng2490
    @jasonleng2490 2 ปีที่แล้ว

    So if I dynamically allocated a array inside a task, does it go onto the task's stack?

  • @lucvanhove9639
    @lucvanhove9639 3 ปีที่แล้ว +4

    Thanks Shawn, you just explain my problem and show the way to a sullution. 👍🧐

  • @fouzaialaa7962
    @fouzaialaa7962 4 หลายเดือนก่อน

    malloc is called that becoz it references the human readable Memory Allocate so it becomes malloc
    i find it easier to memorize the functions name if i know the human readable equivalent .

  • @vikrantgsai7327
    @vikrantgsai7327 2 หลายเดือนก่อน

    If the heap has some bytes remaining that is more than enough to allocate more arrays of 1024 integers in the above example at 9:17 why does it say that the heap has run out of heap?

  • @josephlai7737
    @josephlai7737 3 ปีที่แล้ว +8

    How do you get 768 bytes overhead at 6:22? Shouldn't it be 623 (1024 - 401)?

    • @ShawnHymel
      @ShawnHymel 3 ปีที่แล้ว +22

      Each task in ESP-IDF requires 768 bytes of task stack for bare minimum operation. If you allocate 1024 bytes for stack, that gives you 1024 - 768 = 256 bytes left in stack for that task. Since we try to declare an int array with 100 elements, that will try to use 400 bytes of stack (100 * sizeof(int) = 400 bytes), which is more than we have left. Hope that helps!

    • @josephlai7737
      @josephlai7737 3 ปีที่แล้ว +5

      ​@@ShawnHymel Thanks! Now I see that 768 bytes comes from documentation/configuration.

    • @coderhex1675
      @coderhex1675 3 ปีที่แล้ว +6

      @@ShawnHymel you should have explain like that in the video. Because i have had the same question. Now , i got this but i have wasted my brain tying to figure out.
      Anyway..
      Thank you

    • @jinmingtang1545
      @jinmingtang1545 2 ปีที่แล้ว

      Thank you for pointing it out and bridging the gap!

    • @diyworkshopvn6231
      @diyworkshopvn6231 ปีที่แล้ว

      I have the same question, thank for your pointing it out. :)

  • @debarkamondal6406
    @debarkamondal6406 ปีที่แล้ว

    Why are we counting till idx-1 and why is volatile qualifier necessary?

  • @neilbalch
    @neilbalch 2 ปีที่แล้ว +2

    Interesting note... I wasn't able to get my ESP32-S2-Mini-1 to crash when allocating the 400-byte array when I configured the task to use 1024 bytes of stack memory. I had to lower it to something like 500 bytes to replicate the hard reset. I supposed this means that the overhead is lower for my version of the ESP-IDF core? :)

    • @SohamChakraborty42069
      @SohamChakraborty42069 ปีที่แล้ว +1

      Yes, probably this is what is happening. I suppose the overhead needed would depend on the device

  • @philflupp8199
    @philflupp8199 ปีที่แล้ว +1

    I have completed the challenge. However I could not get my application to run properly without using vTaskDelay(0) in both tasks. Otherwise, only the task which was created first, is executed, and the other one never. The tasks are of the same priority and are pinned to one core. It seems to me that you actually have to explicitly yield task execution (by calling vTaskDelay() ) in order to give other tasks a chance to run. Which contradicts, what was said about the scheduler executing tasks of same priority in round robin fashion ?!

    • @user-kp7im1sg8k
      @user-kp7im1sg8k 8 หลายเดือนก่อน

      I have the same problem...

  • @boontecklee592
    @boontecklee592 2 ปีที่แล้ว +6

    At around 0:34, you mentioned that: "This section is labelled as static memory and connot be used for other parts of the program".
    This explanation is misleading as it gave the impression that static memory cannot be 'used' for other parts of the program. Static memory can certainly be used in any part of program as long as the variable visibility rules are adhered to.
    Instead, your explanation could have been better stated as: "This section is labelled as static memory which is only allocated during compile time".

    • @ShawnHymel
      @ShawnHymel 2 ปีที่แล้ว +2

      Good point, thank you!

    • @Microcontrollerslab
      @Microcontrollerslab 2 ปีที่แล้ว

      What he is trying to say is that the static variables have an internal linkage that means static variables can only be accessed within the same program file where they are defined. You can not access static variables in any other file if your program has multiple files. similarly, if we use static with the function definition, it can not be accessed from any other file.

    • @boontecklee592
      @boontecklee592 2 ปีที่แล้ว +1

      @@Microcontrollerslabit would have been clearer and much more accurate to simply state that static variables follow the same visibility rules as other variables.

  • @philflupp8199
    @philflupp8199 ปีที่แล้ว +1

    Calculating the size of the actually allocated memory gives me 4112 bytes instead of 4 * 1024 = 4096 bytes, why is that?

    • @vex123
      @vex123 11 หลายเดือนก่อน +1

      I was wondering the exact same thing. I also did the exercise and even just allocating one byte, it decreases the heap size by like 20 bytes. Is there some type of overhead in the heap when dynamic allocation is used?

  • @jahidulislamrahat9877
    @jahidulislamrahat9877 3 ปีที่แล้ว +2

    Sorry actually, I did not understand stack size calculation. can you please describe to me how can I calculate it? in my case memory size is 408 bytes.

    • @ShawnHymel
      @ShawnHymel 3 ปีที่แล้ว +1

      To get stack, you need to add up the size of all of the automatically allocated variables for that task.

    • @3adelz90
      @3adelz90 2 ปีที่แล้ว

      @@ShawnHymel How can I calculate thr stack size I need if I have a big Task with lots of functions ?

  • @eylulmood4830
    @eylulmood4830 2 ปีที่แล้ว +3

    please, how did you calculate the overhead 768 at 06:21

    • @digikey
      @digikey  2 ปีที่แล้ว +8

      Shawn Hymel
      6 months ago
      Each task in ESP-IDF requires 768 bytes of task stack for bare minimum operation. If you allocate 1024 bytes for stack, that gives you 1024 - 768 = 256 bytes left in stack for that task. Since we try to declare an int array with 100 elements, that will try to use 400 bytes of stack (100 * sizeof(int) = 400 bytes), which is more than we have left. Hope that helps!

    • @eylulmood4830
      @eylulmood4830 2 ปีที่แล้ว

      @@digikey thank you Mr it helps :)

    • @yadavsomnath6188
      @yadavsomnath6188 ปีที่แล้ว +2

      @@digikey then why it didn't overflowed in first iteration itself ?
      I mean we are not freeing up the space then according to calculation 400>>usable stack..
      So it should not allow this many iteration..rather it should give error at first iteration itself..

  • @eslamsayed1947
    @eslamsayed1947 3 ปีที่แล้ว +1

    How to change task scheduling time in freeRTOS?

    • @ShawnHymel
      @ShawnHymel 3 ปีที่แล้ว +2

      Good question--It's set in the line "#define configTICK_RATE_HZ" in FreeRTOSConfig.h. This is set to "1000" by default in the ESP32 Arduino package.

  • @ymp175
    @ymp175 ปีที่แล้ว +1

    Please can anyone help? Why is the stack canary watchpoint not triggered immediately and the task while loop is able to run several times before the trigger? Also, is it watching the "real" stack or the "task" stack in the heap?

    • @taccle1129
      @taccle1129 ปีที่แล้ว

      Could you find any answer to your question?

    • @yadavsomnath6188
      @yadavsomnath6188 ปีที่แล้ว

      Same question buddy..

    • @ymp175
      @ymp175 ปีที่แล้ว

      @@taccle1129 Nope. I moved on. Better not be stuck. Can always revisit this issue.

    • @pandabar2869
      @pandabar2869 ปีที่แล้ว

      The stack overflow is not checked every time when something is written to the stack, but only when the tick is finished and the kernel takes over control. Then the kernel can check if the canary was overwritten.

    • @ymp175
      @ymp175 ปีที่แล้ว

      @@pandabar2869 Thank you! I think it might take several ticks? Because the loop is able to run 10+ times(not sure, don't have the code at hand).

  • @zulkaifahmed1893
    @zulkaifahmed1893 ปีที่แล้ว

    you look like glassreflection guy who reviews anime

  • @mdmaroof2007
    @mdmaroof2007 3 ปีที่แล้ว +10

    Maybe you need to slow down while explaining 😉

    • @alissonneres5067
      @alissonneres5067 3 ปีที่แล้ว +5

      Good thing it is a video and I can watch again and again if I need (and I usually do)

    • @boontecklee592
      @boontecklee592 2 ปีที่แล้ว +4

      I too feel the narrative is too fast. It tends to distract the listeners and affect understanding. I hope your narrative for future videos can be slower by say 10~15%. Nevertheless, your content is excellent!

    • @rbclima
      @rbclima 2 ปีที่แล้ว +5

      @@boontecklee592 you can always watch at 0.75x!

    • @ashwin372
      @ashwin372 ปีที่แล้ว +3

      use 0.5x for that

    • @pnuema1.618
      @pnuema1.618 ปีที่แล้ว +2

      Slow the video down and open up his code for review before watching the video. That's how you really learn this stuff anyway