/2014/01/01/apr-pools/index.html
HTML | 376 lines | 225 code | 128 blank | 23 comment | 0 complexity | 383cdde45f9d5d295b8b91bf4c6c6434 MD5 | raw file
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>
-
- apr 学习笔记之 apr_pool -
-
- Lot About
- </title>
-
- <meta name="author" content="Mark Wallace" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link href="/assets/css/bootstrap.min.css" rel="stylesheet">
- <link href="/assets/css/bootstrap-responsive.min.css" rel="stylesheet">
- <link href="/assets/css/docs.css" rel="stylesheet">
- <link href="/assets/css-social-buttons/zocial.stripped.css" rel="stylesheet">
- <link href="/assets/js/google-code-prettify/prettify.css" rel="stylesheet">
- <link href="/assets/css/style.css" rel="stylesheet">
-
- <!-- mathjax config similar to math.stackexchange -->
- <script type="text/x-mathjax-config">
- MathJax.Hub.Config({
- jax: ["input/TeX", "output/HTML-CSS"],
- tex2jax: {
- inlineMath: [ ['$', '$'] ],
- displayMath: [ ['$$', '$$']],
- processEscapes: true,
- skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
- },
- messageStyle: "none",
- "HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"] }
- });
- </script>
-
- <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
- <!--[if lt IE 9]>
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></sc\
- ript>
- <![endif]-->
- </head>
- <script type="text/javascript">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-39956831-1']);
- _gaq.push(['_trackPageview']);
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>
- <body data-spy="scroll" data-target=".bs-docs-sidebar">
- <div class="navbar navbar-fixed-top">
- <div class="navbar-inner">
- <div class="container">
- <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a href="/" class="brand">Lot About</a>
- <div class="nav-collapse collapse">
- <ul class="nav">
- <li><a href="/">HOME</a></li>
-
-
- <!-- use sorted_for.rb plugin -->
-
-
-
-
-
-
-
-
-
-
-
-
- <li><a href="/archive.html">Archive</a></li>
-
-
-
-
-
-
- <li><a href="/rss.xml">RSS</a></li>
-
-
-
-
-
-
- <li><a href="/atom.xml">Atom</a></li>
-
-
-
-
-
-
- <li><a href="/about.html">About</a></li>
-
-
-
- </ul>
- <ul class="nav pull-right social visible-desktop">
- <li class="divider-vertical"></li>
-
- <li>
- <a href="https://github.com/lotabout" class="zocial github icon" target="_blank">
- <span class="hidden-desktop">Github</span>
- </a>
- </li>
-
-
- <li>
- <a href="https://plus.google.com/103158895948424209870" class="zocial googleplus icon" target="_blank">
- <span class="hidden-desktop">Google+</span>
- </a>
- </li>
-
-
-
-
-
- </ul>
- </div>
- </div>
- </div>
- </div>
- <div class="container">
- <!--<div class="row-fluid">-->
- <!--<div class="span3"></div>-->
- <!--<div class="span7">-->
- <!--<div class="page-header">-->
- <!--<h1>apr 学习笔记之 apr_pool <br/><small>Apache Portable Routine 学习笔记之内存管理机制:apr_pool</small></h1>-->
- <!--</div>-->
- <!--</div>-->
- <!--<div class="span3"></div>-->
- <!--</div>-->
- <div class="post-header">
- <h1>apr 学习笔记之 apr_pool <br/><small>Apache Portable Routine 学习笔记之内存管理机制:apr_pool</small></h1>
- </div>
- <div class="row-fluid">
- <div class="span3 bs-docs-sidebar">
- <ul class='nav nav-list bs-docs-sidenav' id="toc">
- <!-- toc -->
- </ul>
- </div>
- <div class="span7 main">
- <h1 id="memory-pool">什么是内存池(memory pool)</h1>
- <p>这里给出一个快速的概念,所以内存池,是针对传统动态内存管理机制:<code>malloc</code> 和
- <code>free</code> 提出的。内存池,就是预先向系统申请获得一大块的内存,称之为内存池,之后在获得的内存池之上,管理内存的分配与释放。</p>
- <p>传统上,当我们需要内存时,就调用 <code>malloc</code> 系统函数(严格上说是C的库函数)获得内存,当我们不在需要分配获得的内存时,调用 <code>free</code>系统函数释放它。这种方法有两个主要的缺点:</p>
- <ol>
- <li>当内存分配频繁时,效率较低。</li>
- <li>当内存分配的大小不一时,容易产生内存碎片。</li>
- </ol>
- <p>第一种开销主要是源自调用系统函数开销较大(Linux 下,malloc 会使用系统调用 sbrk
- )。</p>
- <p>内存池是一次性获得一大块内存,当用户需要时内存时,从池中获取,当使用完毕时,归还池中,当有新的请求时,就可以将之前归还的内存重新分配。对比之下,<code>free</code>函数每次都把内存归还给系统,而内存池技术可以重用这些内存,因此省下了许多系统调用的开销。</p>
- <p>另一方面,内存池是以固定大小的内存块进行处理的。例如,<code>malloc</code>可以分配一块大小为 10 Byte 的内存(实际分配的可能是对齐过的,如12或16B)。而内存池是以一个块为单位进行分配,例如每个块大小为 256 Byte, 那即使只需要 10B, 内存池也会为我们分配 256 Byte 即1个块。</p>
- <p>我们可能会觉得这样不是浪费空间嘛。是的,这是我们需要付出的代价。但使用整齐的块,能有效减少内存碎片的产生。</p>
- <p>本节我们对内存池的基本概念进行了介绍,之后会进行一些细节的说明。</p>
- <h1 id="apr-">APR 的内存池使用</h1>
- <h1 id="apr--1">APR 的内存池实现</h1>
- <hr/>
- <div class="pagination btn-group">
-
- <a href="/2013/12/15/image-files-manipulate" class="btn prev" title="Linux 镜像文件处理"> ← Previous </a>
-
- <a href="/archive.html" class="btn">Archive</a>
-
- <a href="/2014/03/27/recursion" class="btn next" title="递归的奥秘"> Next →</a>
-
- </div>
- <hr>
- <div class="post">
- <h2>Related Posts</h2>
- <ul>
-
- <li>
- <span>27 Mar 2014</span>
- »
- <a href="/2014/03/27/recursion">递归的奥秘</a>
- </li>
-
- <li>
- <span>15 Dec 2013</span>
- »
- <a href="/2013/12/15/image-files-manipulate">Linux 镜像文件处理</a>
- </li>
-
- <li>
- <span>26 Sep 2013</span>
- »
- <a href="/2013/09/26/awk-notes">awk 常用命令</a>
- </li>
-
- <li>
- <span>19 May 2013</span>
- »
- <a href="/2013/05/19/how-to-ask">你的问题被鄙视过吗?</a>
- </li>
-
- <li>
- <span>12 Apr 2013</span>
- »
- <a href="/2013/04/12/git-notes">git 常用命令</a>
- </li>
-
- <li>
- <span>08 Apr 2013</span>
- »
- <a href="/2013/04/08/how-is-this-blog-built">论本博客的创建</a>
- </li>
-
- <li>
- <span>07 Apr 2013</span>
- »
- <a href="/2013/04/07/test">Test of Jekyll</a>
- </li>
-
- </ul>
- </div>
- <div id="disqus_thread"></div>
- <script type="text/javascript">
- var disqus_developer = 1;
- var disqus_shortname = 'lotaboutlife'; // required: replace example with your forum shortname
-
- /* * * DON'T EDIT BELOW THIS LINE * * */
- (function() {
- var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
- dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
- })();
- </script>
- <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
- <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
- <!-- add comment here -->
- </div>
- <div class="span2">
- <section>
- <h4>Published</h4>
- <div class="date">
- <span>01 January 2014</span>
- </div>
- </section>
-
-
-
- <section>
- <h4>Categories</h4>
- <ul class="tag_box">
-
-
-
- <li><a href="/categories.html#c-ref">
- c <span>1</span>
- </a></li>
-
- </ul>
- </section>
-
- <section>
- <h4>Tags</h4>
- <ul class="tag_box">
-
-
-
- <li><a href="/tags.html#apr-ref">apr <span>1</span></a></li>
-
- <li><a href="/tags.html#memory_pool-ref">memory_pool <span>1</span></a></li>
-
- </ul>
- </section>
-
- </div>
- </div>
- </div>
- <!-- Footer
- ================================================== -->
- <footer class="footer">
- <div class="container">
- <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>
- <p> Copyright 2013 - 2014 by <a href="https://lotabout.bitbucket.org/about.html"> Mark Wallace </a> </p>
- <ul class="footer-links">
- <li><a href="https://github.com/lotabout">github</a></li>
- <li class="muted">·</li>
- <li><a href="https://bitbucket.org/lotabout">bitbucket</a></li>
- <li class="muted">·</li>
- <li><a href="https://bitbucket.org/lotabout/lotabout.bitbucket.org">Site Source</a></li>
- </ul>
- </div>
- </footer>
- <!-- Le javascript
- ================================================== -->
- <!-- Placed at the end of the document so the pages load faster -->
- <script src="/assets/js/jquery-1.7.1.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- var TOC=""
- $("div.main>h1, div.main>h2").each(function(i){
- var current=$(this);
- current.attr("id", "title" + i);
- current.css("padding-top", "45px"); <!-- solve navbar overlap problem -->
- current.css("margin-top", "-20px"); <!-- solve navbar overlap problem -->
- TOC += "<li><a id='link" + i + "' href='#title" + i + "'title='" + current.attr("tagName") + "'>" + current.text() + "<i class='icon-chevron-right'></i></a></li>"
- })
- $("#toc").append(TOC)
- });
- </script>
- <script type="text/javascript">
- $(document).ready(function(){
- $('pre').addClass('prettyprint linenums') })
- </script>
- <script src="/assets/js/bootstrap.js"></script>
- <script src="/assets/js/holder.js"></script>
- <script src="/assets/js/application.js"></script>
- <script src="/assets/js/google-code-prettify/prettify.js"></script>
-
- <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"></script>
-
- </body>
- </html>