为什么我们不应该在嵌入式系统中具有不同大小的动态分配内存 [英] Why shouldn't we have dynamic allocated memory with different size in embedded system

查看:274
本文介绍了为什么我们不应该在嵌入式系统中具有不同大小的动态分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在嵌入式系统中听说过,我们应该使用一些预先分配的固定大小的内存块(比如好友内存系统?)。有人可以给我详细解释为什么吗
谢谢,

解决方案

在嵌入式系统中,内存非常有限。因此,如果你偶尔只丢失一个字节的内存(因为你分配了它,但是你不会释放它),这会很快地占用系统内存(1 GB的RAM,泄漏率为1 /小时将占用它时间,如果你有4kB的RAM,不要那么长)



本质上避免动态内存的行为是避免程序中的错误的影响。由于静态内存分配是完全确定的(而动态内存分配不是),通过仅使用静态内存分配,可以抵消这种错误。一个重要的因素是嵌入式系统经常用于安全关键应用程序。几个小时的停机时间可能花费数百万美元,或者可能发生事故。



此外,根据动态内存分配器,不确定性也可能需要不确定的时间量可能导致更多的错误,特别是在系统依赖时间紧迫的情况下(感谢 Clifford 提及)。这种类型的错误通常难以测试和复制,因为它依赖于一个非常具体的执行路径。



此外,嵌入式系统通常不具有MMU,因此不像记忆保护如果你的内存不足,你的代码不能处理这个条件,你最终可能会执行任何内存作为指令(不好的事情可能会发生!但是这种情况只是间接地与动态内存分配有关)。



由于郝沉提到,分裂也是一个危险。是否可能发生取决于您的确切用例,但在嵌入式系统中,由于碎片而使50%的RAM变得非常容易。您只能分配总是具有完全相同大小的块的碎片。



性能也扮演一个角色(取决于usecase - 谢谢郝沉)。静态分配的内存由编译器分配,而 malloc()等需要在设备上运行,因此消耗CPU时间(和电源)。



许多嵌入式操作系统(例如ChibiOS)支持某种动态内存分配器。但是使用它只会增加出现意外问题的可能性。



请注意,这些参数通常使用较小的静态分配的内存池来规避。这不是一个真正的解决方案,因为在这些池中仍然可以用尽内存,但它只会影响系统的一小部分。



正如 Stephano Sanfilippo ,某些系统甚至没有足够的资源来支持动态内存分配。



注意:大多数编码标准,包括 JPL编码标准 DO-178B (关键航空电子码 - 谢谢[Stephano Sanfilippo])( https://stackoverflow.com/users/2344584/stefano-sanfilippo )禁止使用malloc。



我还假定 MISRA C标准 禁止 malloc()因为这个论坛帖子 - 但是我无法访问标准本身。


I have heard in embedded system, we should use some preallocated fixed-size memory chunks(like buddy memory system?). Could somebody give me a detailed explanation why? Thanks,

解决方案

In embedded systems you have very limited memory. Therefore, if you occasionally lose only one byte of memory (because you allocate it , but you dont free it), this will eat up the system memory pretty quickly (1 GByte of RAM, with a leak rate of 1/hour will take its time. If you have 4kB RAM, not as long)

Essentially the behaviour of avoiding dynamic memory is to avoid the effects of bugs in your program. As static memory allocation is fully deterministic (while dynamic memory alloc is not), by using only static memory allocation one can counteract such bugs. One important factor for that is that embedded systems are often used in security-critical application. A few hours of downtime could cost millions or an accident could happen.

Furthermore, depending on the dynamic memory allocator, the indeterminism also might take an indeterminate amount of time, which can lead to more bugs especially in systems relying on tight timing (thanks to Clifford for mentioning this). This type of bug is often hard to test and to reproduce because it relies on a very specific execution path.

Additionally, embedded systems don't usually have MMUs, so there is nothing like memory protection. If you run out of memory and your code to handle that condition doesn't work, you could end up executing any memory as instruction (bad things could happen! However this case is only indirectly related to dynamic mem allocation).

As Hao Shen mentioned, fragmentation is also a danger. Whether it may occur depends on your exact usecase, but in embedded systems it is quite easy to loose 50% of your RAM due to fragmentation. You can only avoid fragmentation if you allocate chunks that always have the exact same size.

Performance also plays a role (depends on the usecase - thanks Hao Shen). Statically allocated memory is allocated by the compiler whereas malloc() and similar need to run on the device and therefore consume CPU time (and power).

Many embedded OSs (e.g. ChibiOS) support some kind of dynamic memory allocator. But using it only increases the possibility of unexpected issues to occur.

Note that these arguments are often circumvented by using smaller statically allocated memory pools. This is not a real solution, as one can still run out of memory in those pools, but it will only affect a small part of the system.

As pointed out by Stephano Sanfilippo, some system don't even have enough resources to support dynamic memory allocation.

Note: Most coding standard, including the JPL coding standard and DO-178B (for critical avionics code - thanks [Stephano Sanfilippo])(https://stackoverflow.com/users/2344584/stefano-sanfilippo) forbid the use of malloc.

I also assume the MISRA C standard forbids malloc() because of this forum post -- however I don't have access to the standard itself.

这篇关于为什么我们不应该在嵌入式系统中具有不同大小的动态分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆