以最小的RAM C程序 [英] C program with minimum RAM

查看:139
本文介绍了以最小的RAM C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在C和C ++的应用程序开发编程内存管理。应用程序将在PC上运行。

如果我想让它使用RAM尽可能少运行时的程序,什么是我需要的,而编程?考虑点

下面是根据我的理解两分,但我不肯定的:

(1)用最小的局部变量在main()等功能。
    由于局部变量保存在栈,这是RAM?

(2)而不是局部变量,使用顶部的全局变量。
    由于全局变量保存在未初始化和初始化的ROM区?

感谢。


<格CLASS =h2_lin「>解决方案

1)一般情况下,替代分配栈上分配堆(例如,用的malloc ),这实际上有一个较大的开销,由于簿记/ etc和堆栈已经有了记忆保留,所以分配堆在可能的情况是经常在preferable。另外还有就是堆栈空间少而堆可以接近与虚拟内存和64位地址空间的现代系统无限制。

2)在PC和其它非嵌入式系统,一切都在你的程序进RAM中的,即,它不是闪蒸到一个ROM的类似存储器,所以全局vs本地在这方面并没有帮助。还全局†倾向于活,只要应用程序正在运行,而当地人可以分配和释放(无论是在堆栈或堆)根据需要,并因此preferable


†更准确,也可能存在局部变量与静态的持续时间,并与全球范围内是动态分配的内存的指针变量,因此局部和全局的条件都相当松散的使用在这里。


在一般情况下,现代的台式机/笔记本电脑,甚至手机操作系统正处于内存管理相当不错的,所以你可能不应该试图为你实际上可能做弊大于利微优化的一切。​​

如果你确实需要关闭程序的内存占用,你必须认识到的所有的在程序存储在RAM中,所以你需要减少的数量和大小工作你拥有的东西,而不是试图玩弄自己的位置。在这里您可以在PC上本地存储的东西别的地方是硬盘驱动器,所以存储大量的资源,并有仅在需要时(preferably只有十分必要的部分)加载它们。但请记住,接盘要比内存访问速度较慢的订单,该操作系统还可以交换的东西到磁盘,如果它的存储空间已满。

程序code本身也存储在RAM里,有规模的编译器优化( -Os /输出中许多常见的编译器选项)。还要记住,如果你节省一点变量空间通过编写更复杂的code,努力可由增加code尺寸撤销;节省您的大胜利优化(例如,玉米pressing大量资源将需要增加DECOM pression code,但仍可能产生大量净赢)。动态链接库(和其他资源)的使用还有助于<青霉>整体的系统的内存占用如果相同的库用于通过同时运行多个程序

(请注意,上述一些不嵌入式开发,例如,code适用,静态常量可能的确可以存储在闪存代替RAM等。)

I want to understand the memory management in C and C++ programming for Application Development. Application will run on the PC.

If I want to make a program which uses RAM as less as possible while running, what are the points I need to consider while programming?

Here are two points according to what I understand, but I am not sure:

(1) Use minimum local variables in main() and other functions. As local variables are saved in stack, which is RAM?

(2) Instead of local variables, use global variables on the top. As global variables are saved in the uninitialized and initialized ROM area?

Thanks.

解决方案

1) Generally the alternative to allocating on the stack is allocating on the heap (e.g., with malloc) which actually has a greater overhead due to bookkeeping/etc, and the stack already has memory reserved for it, so allocating on the stack where possible is often preferable. On the other hand there is less space on the stack while the heap can be close to "unlimited" on modern systems with virtual memory and 64-bit address space.

2) On PCs and other non-embedded system, everything in your program goes in RAM, i.e., it is not flashed to a ROM-like memory, so global versus local does not help in that regard. Also globals† tend to "live" as long as the application is running, while locals can be allocated and freed (either on the stack or heap) as required, and are thus preferable.

  † More accurately, there can also be local variables with static duration, and variables with global scope that are pointers to dynamically allocated memory, so the terms local and global are used quite loosely here.


In general, modern desktop/laptop and even mobile operating systems are quite good at managing memory, so you probably shouldn't be trying to micro-optimize everything as you may actually do more harm than good.

If you really do need to bring down the memory footprint of your program, you must realize that everything in the program is stored in RAM, and so you need to work on reducing the number and size of the things you have, rather than trying to juggle their location. The other place where you can store things locally on a PC is the hard drive, so store large resources there and only load them as required (preferably only exactly the parts required). But remember that disk access is orders of magnitude slower than memory access, and that the operating system can also swap things out to disk if its memory gets full.

The program code itself is also stored in RAM, so have your compiler optimize for size (-Os or /Os option in many common compilers). Also remember that if you save a bit of space in variables by writing more complex code, the effort may be undone by the increased code size; save your optimizations for big wins (e.g., compressing large resources will require the added decompression code, but may still yield a large net win). Use of dynamically linked libraries (and other resources) also helps the overall memory footprint of the system if the same library is used by multiple programs running at the same time.

(Note that some of the above does not apply in embedded development, e.g., code and static constants may be indeed be stored in flash instead of RAM, etc.)

这篇关于以最小的RAM C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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