📄 lib0041.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Chapter 5: Automatic Memory Management</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.books24x7.css">
</head>
<body >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0040.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0042.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>
<div class="chapter">
<a name="ch05"></a>
<h1 class="chapter-title"><span class="chapter-titlelabel">Chapter 5: </span>Automatic Memory Management</h1><a name="535"></a><a name="IDX-281"></a>
<p class="para">Automatic memory managers keep track of the memory that is allocated from the heap so that the programmer is absolved of the responsibility. This makes life easier for the programmer. In fact, not only does it make the programmer's job easier, but it also eliminates other nasty problems, like memory leaks and dangling pointers. The downside is that automatic memory managers are much more difficult to build because they must incorporate all the extra bookkeeping functionality.</p>
<table border="0" cellspacing="0" cellpadding="0" class="note">
<tr>
<td valign="top" class="admon-check"></td><td valign="top" class="admon-title">Note </td><td valign="top" class="admon-body">
<p class="first-para">Automatic memory managers are often referred to as <i class="emphasis">garbage collectors</i>. This is because blocks of memory in the heap that were allocated by a program, but which are no longer referenced by the program, are known as garbage. It is the responsibility of a garbage collector to monitor the heap and free garbage so that it can be recycled for other allocation requests.</p>
</td>
</tr>
</table>
<div class="section">
<h2 class="sect2-title">
<a name="536"></a><a name="ch05lev1sec1"></a>Garbage Collection Taxonomy</h2>
<p class="first-para">Taking out the trash is a dance with two steps:</p>
<ol class="orderedlist">
<li class="first-listitem">
<p class="first-para">Identifying garbage in the heap</p>
</li>
<li class="listitem">
<p class="first-para">Recycling garbage once it is found</p>
</li>
</ol>
<p class="para">The different garbage collection algorithms are distinguished in terms of the mechanisms that they use to implement these two steps. For example, garbage can be identified by <i class="emphasis">reference counting</i> or by <i class="emphasis">tracing.</i> Most garbage collectors can be categorized into one of these two types.</p>
<p class="para">Reference counting collectors identify garbage by maintaining a running tally of the number of pointers that reference each block of allocated memory. When the number of references to a particular block of memory reaches zero, the memory is viewed as garbage <a name="537"></a><a name="IDX-282"></a>and reclaimed. There are a number of types of reference counting algorithms, each one implementing its own variation of the counting mechanism (i.e., simple reference counting, deferred reference counting, 1-bit reference counting, etc.).</p>
<p class="para">Tracing garbage collectors traverse the application run-time environment (i.e., registers, stack, heap, data section) in search of pointers to memory in the heap. Think of tracing collectors as pointer hunter-gatherers. If a pointer is found somewhere in the run-time environment, the heap memory that is pointed to is assumed to be "alive" and is not recycled. Otherwise, the allocated memory is reclaimed. There are several subspecies of tracing garbage collectors, including mark-sweep, mark-compact, and copying garbage collectors.</p>
<p class="para">An outline of different automatic memory management approaches is provided in <a class="internaljump" href="#ch05fig01">Figure 5.1</a>.</p>
<div class="figure">
<a name="538"></a><a name="ch05fig01"></a><span class="figuremediaobject"><a href="images/fig310%5F01%5F0%2Ejpg" NAME="IMG_81" target="_parent"><img src="images/fig310_01.jpg" height="163" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 5.1</span></span>
</div>
<p class="para">In this chapter I am going to examine a couple of garbage collection algorithms and offer sample implementations. Specifically, I will implement a garbage collector that uses reference counting and another that uses tracing. As in the <a href="LiB0032.html#418" target="_parent" class="chapterjump">previous chapter</a>, I will present these memory managers as drop-in replacements for the C standard library <span class="fixed">malloc()</span> and <span class="fixed">free()</span> routines.</p>
<p class="last-para">In an attempt to keep the learning threshold low, I will forego extensive optimization and performance enhancements in favor of keeping my source code simple. I am not interested in impressing you with elaborate syntax kung fu; my underlying motivation is to make it easy for you to pick up my ideas and internalize them. If you are interested in taking things to the next level, you can follow up on some of the suggestions and ideas that I discuss at the end of the chapter.</p>
<a></a>
</div>
</div>
</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0040.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0042.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -