PageRenderTime 68ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/2014/01/01/apr-pools/index.html

https://bitbucket.org/lotabout/lotabout.bitbucket.org
HTML | 376 lines | 225 code | 128 blank | 23 comment | 0 complexity | 383cdde45f9d5d295b8b91bf4c6c6434 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>
  6. apr 学习笔记之 apr_pool -
  7. Lot About
  8. </title>
  9. <meta name="author" content="Mark Wallace" />
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <link href="/assets/css/bootstrap.min.css" rel="stylesheet">
  12. <link href="/assets/css/bootstrap-responsive.min.css" rel="stylesheet">
  13. <link href="/assets/css/docs.css" rel="stylesheet">
  14. <link href="/assets/css-social-buttons/zocial.stripped.css" rel="stylesheet">
  15. <link href="/assets/js/google-code-prettify/prettify.css" rel="stylesheet">
  16. <link href="/assets/css/style.css" rel="stylesheet">
  17. <!-- mathjax config similar to math.stackexchange -->
  18. <script type="text/x-mathjax-config">
  19. MathJax.Hub.Config({
  20. jax: ["input/TeX", "output/HTML-CSS"],
  21. tex2jax: {
  22. inlineMath: [ ['$', '$'] ],
  23. displayMath: [ ['$$', '$$']],
  24. processEscapes: true,
  25. skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
  26. },
  27. messageStyle: "none",
  28. "HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"] }
  29. });
  30. </script>
  31. <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
  32. <!--[if lt IE 9]>
  33. <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></sc\
  34. ript>
  35. <![endif]-->
  36. </head>
  37. <script type="text/javascript">
  38. var _gaq = _gaq || [];
  39. _gaq.push(['_setAccount', 'UA-39956831-1']);
  40. _gaq.push(['_trackPageview']);
  41. (function() {
  42. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  43. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  44. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  45. })();
  46. </script>
  47. <body data-spy="scroll" data-target=".bs-docs-sidebar">
  48. <div class="navbar navbar-fixed-top">
  49. <div class="navbar-inner">
  50. <div class="container">
  51. <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  52. <span class="icon-bar"></span>
  53. <span class="icon-bar"></span>
  54. <span class="icon-bar"></span>
  55. </button>
  56. <a href="/" class="brand">Lot About</a>
  57. <div class="nav-collapse collapse">
  58. <ul class="nav">
  59. <li><a href="/">HOME</a></li>
  60. <!-- use sorted_for.rb plugin -->
  61. <li><a href="/archive.html">Archive</a></li>
  62. <li><a href="/rss.xml">RSS</a></li>
  63. <li><a href="/atom.xml">Atom</a></li>
  64. <li><a href="/about.html">About</a></li>
  65. </ul>
  66. <ul class="nav pull-right social visible-desktop">
  67. <li class="divider-vertical"></li>
  68. <li>
  69. <a href="https://github.com/lotabout" class="zocial github icon" target="_blank">
  70. <span class="hidden-desktop">Github</span>
  71. </a>
  72. </li>
  73. <li>
  74. <a href="https://plus.google.com/103158895948424209870" class="zocial googleplus icon" target="_blank">
  75. <span class="hidden-desktop">Google+</span>
  76. </a>
  77. </li>
  78. </ul>
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. <div class="container">
  84. <!--<div class="row-fluid">-->
  85. <!--<div class="span3"></div>-->
  86. <!--<div class="span7">-->
  87. <!--<div class="page-header">-->
  88. <!--<h1>apr 学习笔记之 apr_pool <br/><small>Apache Portable Routine 学习笔记之内存管理机制apr_pool</small></h1>-->
  89. <!--</div>-->
  90. <!--</div>-->
  91. <!--<div class="span3"></div>-->
  92. <!--</div>-->
  93. <div class="post-header">
  94. <h1>apr 学习笔记之 apr_pool <br/><small>Apache Portable Routine 学习笔记之内存管理机制apr_pool</small></h1>
  95. </div>
  96. <div class="row-fluid">
  97. <div class="span3 bs-docs-sidebar">
  98. <ul class='nav nav-list bs-docs-sidenav' id="toc">
  99. <!-- toc -->
  100. </ul>
  101. </div>
  102. <div class="span7 main">
  103. <h1 id="memory-pool">什么是内存池memory pool</h1>
  104. <p>这里给出一个快速的概念所以内存池是针对传统动态内存管理机制<code>malloc</code>
  105. <code>free</code> 提出的内存池就是预先向系统申请获得一大块的内存称之为内存池之后在获得的内存池之上管理内存的分配与释放</p>
  106. <p>传统上当我们需要内存时就调用 <code>malloc</code> 系统函数严格上说是C的库函数获得内存当我们不在需要分配获得的内存时调用 <code>free</code>系统函数释放它这种方法有两个主要的缺点</p>
  107. <ol>
  108. <li>当内存分配频繁时效率较低</li>
  109. <li>当内存分配的大小不一时容易产生内存碎片</li>
  110. </ol>
  111. <p>第一种开销主要是源自调用系统函数开销较大Linux malloc 会使用系统调用 sbrk
  112. </p>
  113. <p>内存池是一次性获得一大块内存当用户需要时内存时从池中获取当使用完毕时归还池中当有新的请求时就可以将之前归还的内存重新分配对比之下<code>free</code>函数每次都把内存归还给系统而内存池技术可以重用这些内存因此省下了许多系统调用的开销</p>
  114. <p>另一方面内存池是以固定大小的内存块进行处理的例如<code>malloc</code>可以分配一块大小为 10 Byte 的内存实际分配的可能是对齐过的如12或16B而内存池是以一个块为单位进行分配例如每个块大小为 256 Byte, 那即使只需要 10B, 内存池也会为我们分配 256 Byte 即1个块</p>
  115. <p>我们可能会觉得这样不是浪费空间嘛是的这是我们需要付出的代价但使用整齐的块能有效减少内存碎片的产生</p>
  116. <p>本节我们对内存池的基本概念进行了介绍之后会进行一些细节的说明</p>
  117. <h1 id="apr-">APR 的内存池使用</h1>
  118. <h1 id="apr--1">APR 的内存池实现</h1>
  119. <hr/>
  120. <div class="pagination btn-group">
  121. <a href="/2013/12/15/image-files-manipulate" class="btn prev" title="Linux 镜像文件处理"> &larr; Previous </a>
  122. <a href="/archive.html" class="btn">Archive</a>
  123. <a href="/2014/03/27/recursion" class="btn next" title="递归的奥秘"> Next &rarr;</a>
  124. </div>
  125. <hr>
  126. <div class="post">
  127. <h2>Related Posts</h2>
  128. <ul>
  129. <li>
  130. <span>27 Mar 2014</span>
  131. &raquo;
  132. <a href="/2014/03/27/recursion">递归的奥秘</a>
  133. </li>
  134. <li>
  135. <span>15 Dec 2013</span>
  136. &raquo;
  137. <a href="/2013/12/15/image-files-manipulate">Linux 镜像文件处理</a>
  138. </li>
  139. <li>
  140. <span>26 Sep 2013</span>
  141. &raquo;
  142. <a href="/2013/09/26/awk-notes">awk 常用命令</a>
  143. </li>
  144. <li>
  145. <span>19 May 2013</span>
  146. &raquo;
  147. <a href="/2013/05/19/how-to-ask">你的问题被鄙视过吗</a>
  148. </li>
  149. <li>
  150. <span>12 Apr 2013</span>
  151. &raquo;
  152. <a href="/2013/04/12/git-notes">git 常用命令</a>
  153. </li>
  154. <li>
  155. <span>08 Apr 2013</span>
  156. &raquo;
  157. <a href="/2013/04/08/how-is-this-blog-built">论本博客的创建</a>
  158. </li>
  159. <li>
  160. <span>07 Apr 2013</span>
  161. &raquo;
  162. <a href="/2013/04/07/test">Test of Jekyll</a>
  163. </li>
  164. </ul>
  165. </div>
  166. <div id="disqus_thread"></div>
  167. <script type="text/javascript">
  168. var disqus_developer = 1;
  169. var disqus_shortname = 'lotaboutlife'; // required: replace example with your forum shortname
  170. /* * * DON'T EDIT BELOW THIS LINE * * */
  171. (function() {
  172. var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  173. dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  174. (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  175. })();
  176. </script>
  177. <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  178. <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
  179. <!-- add comment here -->
  180. </div>
  181. <div class="span2">
  182. <section>
  183. <h4>Published</h4>
  184. <div class="date">
  185. <span>01 January 2014</span>
  186. </div>
  187. </section>
  188. <section>
  189. <h4>Categories</h4>
  190. <ul class="tag_box">
  191. <li><a href="/categories.html#c-ref">
  192. c <span>1</span>
  193. </a></li>
  194. </ul>
  195. </section>
  196. <section>
  197. <h4>Tags</h4>
  198. <ul class="tag_box">
  199. <li><a href="/tags.html#apr-ref">apr <span>1</span></a></li>
  200. <li><a href="/tags.html#memory_pool-ref">memory_pool <span>1</span></a></li>
  201. </ul>
  202. </section>
  203. </div>
  204. </div>
  205. </div>
  206. <!-- Footer
  207. ================================================== -->
  208. <footer class="footer">
  209. <div class="container">
  210. <p> Powered by <a href="https://github.com/mojombo/jekyll">Jekyll</a> & <a href="http://twitter.github.io/bootstrap/index.html"> twitter bootstrap </a> </p>
  211. <p> Copyright 2013 - 2014 by <a href="https://lotabout.bitbucket.org/about.html"> Mark Wallace </a> </p>
  212. <ul class="footer-links">
  213. <li><a href="https://github.com/lotabout">github</a></li>
  214. <li class="muted">&middot;</li>
  215. <li><a href="https://bitbucket.org/lotabout">bitbucket</a></li>
  216. <li class="muted">&middot;</li>
  217. <li><a href="https://bitbucket.org/lotabout/lotabout.bitbucket.org">Site Source</a></li>
  218. </ul>
  219. </div>
  220. </footer>
  221. <!-- Le javascript
  222. ================================================== -->
  223. <!-- Placed at the end of the document so the pages load faster -->
  224. <script src="/assets/js/jquery-1.7.1.min.js"></script>
  225. <script type="text/javascript">
  226. $(document).ready(function() {
  227. var TOC=""
  228. $("div.main>h1, div.main>h2").each(function(i){
  229. var current=$(this);
  230. current.attr("id", "title" + i);
  231. current.css("padding-top", "45px"); <!-- solve navbar overlap problem -->
  232. current.css("margin-top", "-20px"); <!-- solve navbar overlap problem -->
  233. TOC += "<li><a id='link" + i + "' href='#title" + i + "'title='" + current.attr("tagName") + "'>" + current.text() + "<i class='icon-chevron-right'></i></a></li>"
  234. })
  235. $("#toc").append(TOC)
  236. });
  237. </script>
  238. <script type="text/javascript">
  239. $(document).ready(function(){
  240. $('pre').addClass('prettyprint linenums') })
  241. </script>
  242. <script src="/assets/js/bootstrap.js"></script>
  243. <script src="/assets/js/holder.js"></script>
  244. <script src="/assets/js/application.js"></script>
  245. <script src="/assets/js/google-code-prettify/prettify.js"></script>
  246. <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"></script>
  247. </body>
  248. </html>