C中的垃圾收集 [英] Garbage Collection in C

查看:69
本文介绍了C中的垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要

--------

垃圾收集是一种使用收集器管理内存的方法

库。定期或由分配请求触发,

收集器查找未使用的内存块并回收它们。

此内存分配策略已适应C(和C ++)由Hans J Boehm和Alan J Demers编写的

库。


为什么是垃圾收集器?

----- ------------------

标准C只知道malloc / calloc / free函数。程序员

必须管理它分配的每个内存块,永远不要忘记为每个块调用

标准函数free()。任何错误都会立即致b / b
致命,但helas,不会立即造成后果。许多错误,比如

两次(或更多)释放一个块或者忘记释放分配的

块将会在很晚之后发现(如果有的话)。这种类型的错误很难找到,整个行业的软件包

只是为了找到这种类型的错误。


垃圾收集器提供了一种可行的替代方案,可以替代传统的

malloc / free手册。分配策略。 Boehm

的分配器在分配请求完成或者程序员明确调用时,尝试查找未使用的内存。


垃圾收集器的主要优点是程序员可以从分配/释放内存的责任中解脱出来。

程序员向GC请求内存,然后其余为*自动*。

GC的限制。

---- -----------------

GC需要查看程序中的所有指针。由于它会周期性地扫描内存,因此它会假设其阻止列表中的任何块都是

,当它找不到任何指针时可以自由重用。这意味着

程序员不能在磁盘中存储指针,或者在windows extra

bytes中存储指针,因为在旧窗口下通常会这样做版本,或者

其他地方。


这实际上不是限制,因为大多数程序都没有写入指向磁盘的
指针,期待它们在以后有效...

显然,有一种无限的方法可以隐藏指针(例如通过对它们进行异或运算来使用某些常量)来隐藏它们。收藏家。


这没有实际意义。指针在正常的

程序中没有被异常,如果你保持在处理器的正常对齐要求之内,一切都没有任何问题。


性能考虑

--------------------------

在现代工作站中,在中等规模项目中进行全面扫描所需的时间非常短,以毫秒为单位。在
非实时的程序中,GC时间完全无法检测到。

我在lcc-win32的IDE中使用过Boehm的GC,特别是在

调试器。我在自动中显示的每个字符串。窗口使用GC分配

。在慢速机器中,你有时会看到停顿时间不到一秒钟,完全无法检测到,除非你知道那是

并试着找到它。


必须要说的是,malloc / free系统也很慢,因为每个分配请求malloc必须通过免费区块列表

试图找到一个免费的。内存也必须合并,以避免分裂,并且malloc调用可能变得非常昂贵,取决于实现和程序完成的分配模式。

。 />
便携性

-----------

Boehm的GC在大多数标准PC和UNIX / Linux平台下运行。

收集器应该适用于Linux,* BSD,最近的Windows版本,MacOS X,
HP / UX,Solaris,Tru64,Irix和其他一些操作系统。一些

端口比其他端口更精致。有关将收集器移植到新平台的说明。 Kenjiro Taura,Toshio Endo和Akinori

Yonezawa已经提供了一个并行收藏家。


结论

----- ------

对于C

(和C ++),GC是传统分配策略的一个很好的替代方案。 malloc / free系统的主要弱点在于它不会扩展。在100%的时间内做任何错误的麻烦任务都是不可能的。

没有任何错误。你可以擅长它,你可以对它感到尴尬,但你永远不会完美。这是人性。


GC让你摆脱这些问题,并允许你在

中记录真正重要的问题,以及你可以在哪里展示你的实力

作为软件设计师。它让你摆脱了无聊的任务,即跟踪你分配的每个内存块的价值。


jacob

Abstract
--------
Garbage collection is a method of managing memory by using a "collector"
library. Periodically, or triggered by an allocation request, the
collector looks for unused memory chunks and recycles them.
This memory allocation strategy has been adapted to C (and C++) by the
library written by Hans J Boehm and Alan J Demers.

Why a Garbage Collector?
-----------------------
Standard C knows only the malloc/calloc/free functions. The programmer
must manage each block of memory it allocates, never forgetting to call
the standard function free() for each block. Any error is immediately
fatal, but helas, not with immediate consequences. Many errors like
freeing a block twice (or more) or forgetting to free an allocated
block will be discovered much later (if at all). This type of bugs are
very difficult to find and a whole industry of software packages
exists just to find this type of bugs.

The garbage collector presents a viable alternative to the traditional
malloc/free "manual" allocation strategies. The allocator of Boehm
tries to find unused memory when either an allocation request is
done, or when explicitely invoked by the programmer.

The main advantage of a garbage collector is that the programmer is
freed from the responsability of allocating/deallocating memory. The
programmer requests memory to the GC, and then the rest is *automatic*.
Limitations of the GC.
---------------------
The GC needs to see all pointers in a program. Since it scans
periodically memory, it will assume that any block in its block list is
free to reuse when it can''t find any pointers to it. This means that the
programmer can''t store pointers in the disk, or in the "windows extra
bytes", as it was customary to do under older windows versions, or
elsewhere.

This is actually not a limitation since most programs do not write
pointers to disk, and expect them to be valid later...
Obviously, there is an infinite way to hide pointers (by XORing them
with some constant for instance) to hide them from the collector.

This is of no practical significance. Pointers aren''t XORed in normal
programs, and if you stay within the normal alignment requirements
of the processor, everything works without any problems.

Performance considerations
--------------------------
In modern workstations, the time needed to make a complete sweep in
mid-size projects is very small, measured in some milliseconds. In
programs that are not real time the GC time is completely undetectable.
I have used Boehm''s GC in the IDE of lcc-win32, specially in the
debugger. Each string I show in the "automatic" window is allocated
using the GC. In slow machines you can sometimes see a pause of
less than a second, completely undetectable unless you know that is
there and try to find it.

It must be said too that the malloc/free system is slow too, since at
each allocation request malloc must go through the list of free blocks
trying to find a free one. Memory must be consolidated too, to avoid
fragmentation, and a malloc call can become very expensive, depending
on the implementation and the allocation pattern done by the program.
Portability
-----------
Boehm''s GC runs under most standard PC and UNIX/Linux platforms. The
collector should work on Linux, *BSD, recent Windows versions, MacOS X,
HP/UX, Solaris, Tru64, Irix and a few other operating systems. Some
ports are more polished than others. There are instructions for porting
the collector to a new platform. Kenjiro Taura, Toshio Endo, and Akinori
Yonezawa have made available a parallel collector.

Conclusions
-----------
The GC is a good alternative to traditional allocation strategies for C
(and C++). The main weakness of the malloc/free system is that it
doesn''t scale. It is impossible to be good at doing a mind numbing task
without any error 100% of the time. You can be good at it, you can be
bad at it, but you can NEVER be perfect. It is human nature.

The GC frees you from those problems, and allows you to conecntrate in
the problems that really matter, and where you can show your strength
as software designer. It frees you from the boring task of keeping track
of each memory block you allocate.

jacob

推荐答案

jacob navia说:
jacob navia said:

为什么垃圾收集器?

---- -------------------

标准C只知道malloc / calloc / free函数。
Why a Garbage Collector?
-----------------------
Standard C knows only the malloc/calloc/free functions.



相当如此。请将关于非C事项的讨论转移到其他新闻组

哪里是热门话题。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

Quite so. Please move discussions of non-C matters to some other newsgroup
where it is topical.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


in 700822 20061011 175810 Richard Heathfield< in *****@invalid.invalidwrote:
in 700822 20061011 175810 Richard Heathfield <in*****@invalid.invalidwrote:

> jacob navia说:
>jacob navia said:

>为什么是垃圾收集器?
-----------------------
标准C只知道malloc / calloc / free函数。
>Why a Garbage Collector?
-----------------------
Standard C knows only the malloc/calloc/free functions.


非常好。请将关于非C事项的讨论转移到其他新闻组
这是主题。


Quite so. Please move discussions of non-C matters to some other newsgroup
where it is topical.



你是否告诉雅各布远离你的围栏?

Are you telling Jacob to stay out of your playpen?


Bob Martin说:
Bob Martin said:

in 700822 20061011 175810 Richard Heathfield< in ***** @ invalid.invalid>

写道:
in 700822 20061011 175810 Richard Heathfield <in*****@invalid.invalid>
wrote:

>> jacob navia说:
>>jacob navia said:

>>为什么是垃圾收集器?
------ -----------------
标准C只知道malloc / calloc / free函数。
>>Why a Garbage Collector?
-----------------------
Standard C knows only the malloc/calloc/free functions.


非常好。请将关于非C事项的讨论转移到其他新闻组
这是主题。


Quite so. Please move discussions of non-C matters to some other newsgroup
where it is topical.



你是否告诉雅各布远离你的围栏?


Are you telling Jacob to stay out of your playpen?



不,我要求他遵守Usenet的时事惯例。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

No, I''m asking him to observe the topicality conventions of Usenet.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于C中的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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