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

  Рет қаралды 145,744

DigiKey

DigiKey

Күн бұрын

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

Пікірлер: 72
@AlphaCrucis
@AlphaCrucis Жыл бұрын
"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 Жыл бұрын
This channel is basically covering one of my courses thats painfully unclear, absolutely awesome
@Apocobat
@Apocobat Жыл бұрын
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.
@Astronomer6573
@Astronomer6573 2 жыл бұрын
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!
@turkey343434
@turkey343434 3 жыл бұрын
Excellent content. Thank you Shawn, keep them coming!
@minefacex
@minefacex 2 жыл бұрын
This guy is seriously amazing! Thanks for the vid!
@Dygear
@Dygear 3 жыл бұрын
Nice Mission Impossible reference! Your voice does remind me of the guy on the tape, now that you said it.
@ShawnHymel
@ShawnHymel 3 жыл бұрын
I was hoping someone would catch that :)
@adityasreeram.k.s9527
@adityasreeram.k.s9527 6 ай бұрын
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 6 ай бұрын
I found that too..
@achalasamapriya2701
@achalasamapriya2701 3 жыл бұрын
was waiting for this.. thanks
@zee-lab-electronics
@zee-lab-electronics 3 жыл бұрын
Awesome work Shawn
@shubhamsen4572
@shubhamsen4572 3 жыл бұрын
This guy is excellent
@MuhammadDaudkhanTV100
@MuhammadDaudkhanTV100 3 жыл бұрын
Really good ideas
@baobuiquoc4763
@baobuiquoc4763 3 жыл бұрын
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 3 жыл бұрын
thank you very much!!!!!
@perlmansoong944
@perlmansoong944 2 жыл бұрын
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 :-)
@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?
@Hasan...
@Hasan... 3 жыл бұрын
Shawn, are you planning to upload a quick tutorial that uses both cores of ESP32 soon?
@ShawnHymel
@ShawnHymel 3 жыл бұрын
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
@jasonleng2490
@jasonleng2490 3 жыл бұрын
So if I dynamically allocated a array inside a task, does it go onto the task's stack?
@lucvanhove9639
@lucvanhove9639 3 жыл бұрын
Thanks Shawn, you just explain my problem and show the way to a sullution. 👍🧐
@debarkamondal6406
@debarkamondal6406 2 жыл бұрын
Why are we counting till idx-1 and why is volatile qualifier necessary?
@neilbalch
@neilbalch 3 жыл бұрын
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 Жыл бұрын
Yes, probably this is what is happening. I suppose the overhead needed would depend on the device
@vikrantgsai7327
@vikrantgsai7327 4 ай бұрын
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 жыл бұрын
How do you get 768 bytes overhead at 6:22? Shouldn't it be 623 (1024 - 401)?
@ShawnHymel
@ShawnHymel 3 жыл бұрын
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 жыл бұрын
​@@ShawnHymel Thanks! Now I see that 768 bytes comes from documentation/configuration.
@coderhex1675
@coderhex1675 3 жыл бұрын
@@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. :)
@fouzaialaa7962
@fouzaialaa7962 7 ай бұрын
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 .
@philflupp8199
@philflupp8199 Жыл бұрын
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 10 ай бұрын
I have the same problem...
@mudkiptheengineer5339
@mudkiptheengineer5339 10 ай бұрын
check if you have configUSE_PREEMPTION 1, this will make the scheduler round Robin through all the tasks of the same priority that are in the ready state
@eslamsayed1947
@eslamsayed1947 3 жыл бұрын
How to change task scheduling time in freeRTOS?
@ShawnHymel
@ShawnHymel 3 жыл бұрын
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.
@eylulmood4830
@eylulmood4830 2 жыл бұрын
please, how did you calculate the overhead 768 at 06:21
@digikey
@digikey 2 жыл бұрын
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 Жыл бұрын
@@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..
@jahidulislamrahat9877
@jahidulislamrahat9877 3 жыл бұрын
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 жыл бұрын
To get stack, you need to add up the size of all of the automatically allocated variables for that task.
@3adelz90
@3adelz90 3 жыл бұрын
@@ShawnHymel How can I calculate thr stack size I need if I have a big Task with lots of functions ?
@philflupp8199
@philflupp8199 Жыл бұрын
Calculating the size of the actually allocated memory gives me 4112 bytes instead of 4 * 1024 = 4096 bytes, why is that?
@vex123
@vex123 Жыл бұрын
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?
@MingOnUTube
@MingOnUTube Жыл бұрын
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..
@MingOnUTube
@MingOnUTube Жыл бұрын
@@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.
@MingOnUTube
@MingOnUTube Жыл бұрын
@@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).
@boontecklee592
@boontecklee592 3 жыл бұрын
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 3 жыл бұрын
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 жыл бұрын
@@Microcontrollerslabit would have been clearer and much more accurate to simply state that static variables follow the same visibility rules as other variables.
@zulkaifahmed1893
@zulkaifahmed1893 2 жыл бұрын
you look like glassreflection guy who reviews anime
@mdmaroof2007
@mdmaroof2007 3 жыл бұрын
Maybe you need to slow down while explaining 😉
@alissonneres5067
@alissonneres5067 3 жыл бұрын
Good thing it is a video and I can watch again and again if I need (and I usually do)
@boontecklee592
@boontecklee592 3 жыл бұрын
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 жыл бұрын
@@boontecklee592 you can always watch at 0.75x!
@ashwin372
@ashwin372 Жыл бұрын
use 0.5x for that
@pnuema1.618
@pnuema1.618 Жыл бұрын
Slow the video down and open up his code for review before watching the video. That's how you really learn this stuff anyway
Introduction to RTOS Part 5 - Queue | Digi-Key Electronics
12:01
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 8 МЛН
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 61 МЛН
Understanding Static, Stack, and Heap Memory Regions (Examples in C)
33:29
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,4 МЛН
But, what is Virtual Memory?
20:11
Tech With Nikola
Рет қаралды 239 М.
Stack vs Heap Memory - Simple Explanation
5:28
Alex Hyett
Рет қаралды 208 М.
i wrote my own memory allocator in C to prove a point
5:23
Low Level Learning
Рет қаралды 357 М.
S24 Ultra and IPhone 14 Pro Max telephoto shooting comparison #shorts
0:15
Photographer Army
Рет қаралды 9 МЛН
Самые крутые школьные гаджеты
0:49
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,9 МЛН
Что делать если в телефон попала вода?
0:17
Лена Тропоцел
Рет қаралды 2,5 МЛН
Как распознать поддельный iPhone
0:44
PEREKUPILO
Рет қаралды 2,1 МЛН