heaptrc.tex 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1998, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The HEAPTRC unit.}
  22. This chapter describes the HEAPTRC unit for \fpc. It was written by
  23. Pierre Muller.
  24. \section{Purpose}
  25. The HEAPTRC unit can be used to debug your memory allocation/deallocation.
  26. It keeps track of the calls to getmem/freemem, and, implicitly, of
  27. New/Dispose statements.
  28. When the program exits, or when you request it explicitly.
  29. It displays the total memory used, and then dumps a list of blocks that
  30. were allocated but not freed. It also displays where the memory was
  31. allocated.
  32. If there are any inconsistencies, such as memory blocks being allocated
  33. or freed twice, or a memory block that is released but with wrong size,
  34. this will be displayed also.
  35. The information that is stored/displayed can be customized using
  36. some constants.
  37. \section{Usage}
  38. All that you need to do is to include \file{heaptrc} in the uses clause
  39. of your program. Make sure that it is the first unit in the clause,
  40. otherwise memory allocated in initialization code of units that precede the
  41. heaptrc unit will not be accounted for, causing an incorrect memory usage
  42. report.
  43. The following example shows how to use the heaptrc unit.
  44. \latex{\lstinputlisting{heapex/heapex.pp}}
  45. \html{\input{heapex/heapex.tex}}
  46. This is the memory dump shown when running this program:
  47. \begin{verbatim}
  48. Marked memory at 08052C48 invalid
  49. Wrong size : 128 allocated 64 freed
  50. 0x0804C29C
  51. 0x080509E2
  52. 0x080480A4
  53. 0x00000000
  54. Heap dump by heaptrc unit
  55. 13 memory blocks allocated : 1416/1424
  56. 6 memory blocks freed : 708/712
  57. 7 unfreed memory blocks : 708
  58. True heap size : 2097152
  59. True free heap : 2096040
  60. Should be : 2096104
  61. Call trace for block 0x08052C48 size 128
  62. 0x080509D6
  63. 0x080480A4
  64. Call trace for block 0x08052B98 size 128
  65. 0x08050992
  66. 0x080480A4
  67. Call trace for block 0x08052AE8 size 128
  68. 0x08050992
  69. 0x080480A4
  70. Call trace for block 0x08052A38 size 128
  71. 0x08050992
  72. 0x080480A4
  73. Call trace for block 0x08052988 size 128
  74. 0x08050992
  75. 0x080480A4
  76. Call trace for block 0x080528D8 size 128
  77. 0x08050992
  78. 0x080480A4
  79. Call trace for block 0x080528A0 size 4
  80. 0x08050961
  81. 0x080480A4
  82. \end{verbatim}
  83. \section{Constants, Types and variables}
  84. The \var{FillExtraInfoType} is a procedural type used in the
  85. \seep{SetExtraInfo} call.
  86. \begin{lstlisting}{}
  87. type
  88. FillExtraInfoType = procedure(p : pointer);
  89. \end{lstlisting}{}
  90. The following typed constants allow to fine-tune the standard dump of the
  91. memory usage by \seep{DumpHeap}:
  92. \begin{lstlisting}{}
  93. const
  94. tracesize = 8;
  95. quicktrace : boolean = true;
  96. HaltOnError : boolean = true;
  97. keepreleased : boolean = false;
  98. \end{lstlisting}{}
  99. \var{Tracesize} specifies how many levels of calls are displayed of the
  100. call stack during the memory dump. If you specify \var{keepreleased:=True}
  101. then half the \var{TraceSize} is reserved for the \var{GetMem} call stack,
  102. and the other half is reserved for the \var{FreeMem} call stack.
  103. For example, the default value of 8 will cause eight levels of call frames
  104. to be dumped for the getmem call if \var{keepreleased} is \var{False}. If
  105. \var{KeepReleased} is true, then 4 levels of call frames will be dumped for
  106. the \var{GetMem} call and 4 frames wil be dumped for the \var{FreeMem} call.
  107. If you want to change this value, you must recode the \file{heaptrc} unit.
  108. \var{Quicktrace} determines whether the memory manager checks whether a
  109. block that is about to be released is allocated correctly. This is a rather
  110. time consuming search, and slows program execution significantly, so by
  111. default it is set to \var{False}.
  112. If \var{HaltOnError} is set to \var{True} then an illegal call to
  113. \var{FreeMem} will cause the memory manager to execute a \var{halt(1)}
  114. instruction, causing a memory dump. By Default it is set to \var{True}.
  115. If \var{keepreleased} is set to true, then a list of freed memory
  116. blocks is kept. This is useful if you suspect that the same memory block is
  117. released twice. However, this option is very memory intensive, so use it
  118. sparingly, and only when it's really necessary.
  119. \section{Functions and procedures}
  120. \begin{procedure}{DumpHeap}
  121. \Declaration
  122. procedure DumpHeap;
  123. \Description
  124. \var{DumpHeap} dumps to standard output a summary of memory usage.
  125. It is called automatically by the heaptrc unit when your program exits
  126. (by instaling an exit procedure), but it can be called at any time
  127. \Errors
  128. None.
  129. \SeeAlso
  130. \seep{MarkHeap}
  131. \end{procedure}
  132. \begin{procedure}{MarkHeap}
  133. \Declaration
  134. procedure MarkHeap;
  135. \Description
  136. \var{MarkHeap} marks all memory blocks with a special signature.
  137. You can use this if you think that you corruped the memory.
  138. \Errors
  139. None.
  140. \SeeAlso
  141. \seep{DumpHeap}
  142. \end{procedure}
  143. \begin{procedure}{SetExtraInfo}
  144. \Declaration
  145. procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
  146. \Description
  147. You can use \var{SetExtraInfo} to store extra info in the blocks that
  148. the heaptrc unit reserves when tracing getmem calls. \var{Size} indicates the
  149. size (in bytes) that the trace mechanism should reserve for your extra
  150. information. For each call to \var{getmem}, \var{func} will be called,
  151. and passed a pointer to the memory reserved.
  152. When dumping the memory summary, the extra info is shown as Longint values.
  153. \Errors
  154. You can only call \var{SetExtraInfo} if no memroy has been allocated
  155. yet. If memory was already allocated prior to the call to
  156. \var{SetExtraInfo}, then an error will be displayed on standard error
  157. output, and a \seep{DumpHeap} is executed.
  158. \SeeAlso
  159. \seep{DumpHeap}
  160. \end{procedure}
  161. \latex{\lstinputlisting{heapex/setinfo.pp}}
  162. \html{\input{heapex/setinfo.tex}}
  163. %
  164. % $Log$
  165. % Revision 1.3 1999-06-25 22:12:16 michael
  166. % + Update to version 0.19 of listings package
  167. %
  168. % Revision 1.2 1998/12/15 23:50:52 michael
  169. % * Some updates
  170. %
  171. % Revision 1.1 1998/12/14 23:17:02 michael
  172. % + INitial implementation
  173. %
  174. %