page415.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 80 行

HTML
80
字号
<HTML><HEAD><TITLE>Garbage Collection and the Other Kind of Heap</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html5953" HREF="page416.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5951" HREF="book.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5945" HREF="page414.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5955" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0013000000000000000000">Garbage Collection and the Other Kind of Heap</A></H1><P><A NAME="chapgarbage">&#160;</A><P>A Python object is an instance of a Python type.For example, all integers have the type <tt>int</tt>,all floating-point numbers have the type <tt>float</tt>,all user-defined classes have the type <tt>classobj</tt>, andall instances of user-defined classes have the type <tt>instance</tt>.Every object in a Python program occupies some memory.The manner in which a Python object is represented in memoryis left up to the implementor of the Python virtual machine and,in principle, can vary from one implementation to another.However,object data typically occupy contiguous memory locations.<P>The region of memory in which objects are allocated dynamically isoften called <em>a heap</em><A NAME=29781>&#160;</A>.In Chapter&nbsp;<A HREF="page351.html#chappqueues"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we consider <em>heaps</em> and <em>heap-ordered trees</em>in the context of priority queue implementations.Unfortunately, the only thing that the heaps of Chapter&nbsp;<A HREF="page351.html#chappqueues"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>and the heap considered here have in common is the name.While it may be possible to use a heap(in the sense of Definition&nbsp;<A HREF="page352.html#defnheap"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>)to manage a region of memory,typical implementations do not.In this context the technical meaning of the term <em>heap</em>is closer to its dictionary definition--``a pile of many things.''<P>The amount of memory required to represent a Python object isdetermined by its type.For example,four bytes are used typically to represent the value of an <tt>int</tt>,eight bytes are used typically to represent the value of a <tt>float</tt>,24 bytes are used typically to represent the value of a <tt>classobj</tt>, and12 bytes are used typically to represent the value of a <tt>instance</tt>.In addition to the memory required for the value of an object,there is a fixed,constant amount of extra storage set aside in every object (eight bytes).This extra storage carries information used by the Python virtual machineto represent the type of the objectand to aid the process of garbage collection.<P>When the Python virtual machine creates an object,it performs the following steps:<OL><LI>	An unused region of memory large enough	to hold an instance of the desired class is found.<LI>	All of the fields of the object are assigned	their default initial values.<LI>	The appropriate <tt>__init__</tt> method is run	to initialize the object instance.<LI>	A reference to the newly created object is returned.</OL><BR> <HR><UL> <LI> <A NAME="tex2html5956" HREF="page416.html#SECTION0013100000000000000000">What is Garbage?</A><LI> <A NAME="tex2html5957" HREF="page422.html#SECTION0013200000000000000000">Reference Counting Garbage Collection</A><LI> <A NAME="tex2html5958" HREF="page425.html#SECTION0013300000000000000000">Mark-and-Sweep Garbage Collection</A><LI> <A NAME="tex2html5959" HREF="page427.html#SECTION0013400000000000000000">Stop-and-Copy Garbage Collection</A><LI> <A NAME="tex2html5960" HREF="page429.html#SECTION0013500000000000000000">Mark-and-Compact Garbage Collection</A><LI> <A NAME="tex2html5961" HREF="page431.html#SECTION0013600000000000000000">Exercises</A><LI> <A NAME="tex2html5962" HREF="page432.html#SECTION0013700000000000000000">Projects</A></UL><HR><A NAME="tex2html5953" HREF="page416.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5951" HREF="book.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5945" HREF="page414.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5955" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?