📄 malloc.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Dynamic Memory Allocation In SQLite</title><style type="text/css">body { margin: auto; font-family: "Verdana" "sans-serif"; padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline { float:right; text-align:right; font-style:italic; width:240px; margin:12px; margin-top:58px;}.toolbar { font-variant: small-caps; text-align: center; line-height: 1.6em; margin: 0; padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se { background: url(images/se.png) 100% 100% no-repeat #80a796}.sw { background: url(images/sw.png) 0% 100% no-repeat }.ne { background: url(images/ne.png) 100% 0% no-repeat }.nw { background: url(images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head><body><div><!-- container div to satisfy validator --><a href="index.html"><img class="logo" src="images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td> <div class="se"><div class="sw"><div class="ne"><div class="nw"> <div class="toolbar"> <a href="about.html">About</a> <a href="sitemap.html">Sitemap</a> <a href="docs.html">Documentation</a> <a href="download.html">Download</a> <a href="copyright.html">License</a> <a href="news.html">News</a> <a href="http://www.sqlite.org/cvstrac/index">Developers</a> <a href="support.html">Support</a> </div></div></div></div></div></td></tr></table> <h1>Dynamic Memory Allocation In SQLite</h1><p>SQLite uses of dynamic memory allocation to obtainmemory for storing various objects(ex: <a href="c3ref/sqlite3.html">database connections</a> and <a href="c3ref/stmt.html">prepared statements</a>) and to builda memory cache of the database file and to hold the results of queries.Much effort has gone into making the dynamic memory allocation subsystemof SQLite reliable, predictable, robust, and efficient.</p><p>This document provides an overview of dynamic memory allocation within SQLite. The target audience is software engineers who are tuning theiruse of SQLite for peak performance in demanding environments.Nothing in this document is required knowledge for using SQLite. Thedefault settings and configuration for SQLite will work well in mostapplications. However, the information contained in this document maybe useful to engineers who are tuning SQLite to comply with specialrequirements or to run under unusual circumstances.</p><p><i>This report is a work in progress...</i></p><a name="features"></a><h2>1.0 Features</h2><p>The SQLite core and its memory allocation subsystem provides the following capabilities:</p><ul><li><p><b>Robust against allocation failures.</b>If a memory allocation ever fails (that is to say, if malloc() or realloc() ever return NULL)then SQLite will recover gracefully. SQLite will first attemptto free memory from unpinned cache pages then retry the allocationrequest. Failing that, SQLite will either stop whatit is doing and return the<a href="c3ref/c_abort.html">SQLITE_NOMEM</a> error code back up to the application or it willmake due without the requested memory.</p></li><li><p><b>No memory leaks.</b>The application is responsible for destroying any objects it allocates.(For example, the application must use <a href="c3ref/finalize.html">sqlite3_finalize()</a> on every <a href="c3ref/stmt.html">prepared statement</a> and <a href="c3ref/close.html">sqlite3_close()</a> on every <a href="c3ref/sqlite3.html">database connection</a>.) But as long asthe application cooperates, SQLite will never leak memory. This istrue even in the face of memory allocation failures or other systemerrors.</p></li><li><p><b>Memory usage limits.</b>The <a href="c3ref/soft_heap_limit.html">sqlite3_soft_heap_limit()</a> mechanism allows the application toset a memory usage limit that SQLite strives to stay below. SQLitewill attempt to reuse memory from its caches rather than allocation newmemory as it approaches the soft limit.</p></li><li><p><b>Zero-malloc option</b>The application can provide SQLite with several buffers of bulk memoryat startup and SQLite will then use those provided buffers for all ofits memory allocation needs and never call system malloc() or free().</p></li><li><p><b>Application-supplied memory allocators.</b>The application can provide SQLite with pointers to alternative memory allocators at start-time. The alternative memory allocatorwill be used in place of system malloc() and free().</p></li><li><p><b>Proof against breakdown and fragmentation.</b>SQLite can be configured so that, subject to certain usage constraintsdetailed below, it is guaranteed to never fail a memory allocationor fragment the heap.This property is important to long-running, high-reliabilityembedded systems where a memory allocation error could contributeto an overall system failure.</p></li><li><p><b>Memory usage statistics.</b>Applications can see how much memory they are using and detect whenmemory usage is approaching or exceeding design boundaries.</p></li><li><p><b>Minimal calls to the allocator.</b>The system malloc() and free() implementations are inefficienton many systems. SQLite strives to reduce overall processing timeby minimizing its use of malloc() and free().</p></li><li><p><b>Open access.</b>Pluggable SQLite extensions or even the application itself can access to the same underlying memory allocationroutines used by SQLite through the<a href="c3ref/free.html">sqlite3_malloc()</a>, <a href="c3ref/free.html">sqlite3_realloc()</a>, and <a href="c3ref/free.html">sqlite3_free()</a> interfaces.</p></li></ul><a name="testing"></a><h2>2.0 Testing</h2><p>Over75% of the code in the SQLite source tree is devoted purely to testingand verification. Reliability is important to SQLite.Among the tasks of the test infrastructure is to insure thatSQLite does not misuse dynamically allocated memory, that SQLitedoes not leak memory, and that SQLite respondscorrectly to a dynamic memory allocation failure.</p><p>The test infrastructure verifies that SQLite does not misusedynamically allocated memory by using a specially instrumentedmemory allocator. The instrumented memory allocator is enabledat compile-time using the <a href="compile.html#memdebug">SQLITE_MEMDEBUG</a> option. The instrumentedmemory allocator is much slower than the default memory allocator andso its use is not recommended in production. But whenenabled during testing, the instrumented memory allocator performs the following checks:</p><ul><li><p><b>Bounds checking.</b>The instrumented memory allocator places sentinel values at both endsof each memory allocation to verify that nothing within SQLite writesoutside the bounds of the allocation.</p></li><li><p><b>Use of memory after freeing.</b>When each block of memory is freed, every byte is overwritten with anonsense bit pattern. This helps to insure that no memory is everused after having been freed.</p></li><li><p><b>Freeing memory not obtained from malloc.</b>Each memory allocation from the instrumented memory allocator containssentinels used to verify that every allocation freed camefrom prior malloc.</p></li><li><p><b>Uninitialized memory.</b>The instrumented memory allocator initializes each memory allocationto a nonsense bit pattern to help insure that the user makes noassumptions about the content of allocation memory.</p></li></ul><p>Regardless of whether or not the instrumented memory allocator isused, SQLite keeps track of how much memory is currently checked out.There are hundreds of test scripts used for testing SQLite. At theend of each script, all objects are destroyed and a test is made toinsure that all memory has been freed. This is how memoryleaks are detected. Notice that memory leak detection is in force atall times, during test builds and during production builds. Wheneverone of the developers runs any individual test script, memory leakdetection is active. Hence memory leaks that do arise during developmentare quickly detected and fixed.</p><a name="oomtesting"></a><p>The response of SQLite to out-of-memory (OOM) errors is tested usinga specialized memory allocator overlay that can simulate memory failures.The overlay is a layer that is inserted in between the memory allocatorand the rest of SQLite. The overlay passes most memory allocationrequests straight through to the underlying allocator and passes theresults back up to the requester. But the overlay can be set to cause the Nth memory allocation to fail. To run an OOM test, the overlayis first set to fail on the first allocation attempt. Then some testscript is run and verification that the allocation was correctly caughtand handled is made. Then the overlay is set to fail on the secondallocation and the test repeats. The failure point continues to adviceone allocation at a time until the entire test procedure runs tocompletion without hitting a memory allocation error. This wholetest sequence run twice. On the first pass, theoverlay is set to fail only the Nth allocation. On the second pass,the overlay is set to fail the Nth and all subsequent allocations.</p><p>Note that the memory leak detection logic continues to work evenwhen the OOM overlay is being used. This verifies that SQLitedoes not leak memory even when it encounters memory allocation errors.Note also that the OOM overlay can work with any underlying memoryallocator, including the instrumented memory allocator that checksfor memory allocation misuse. In this way it is verified that OOM errors do not induce other kinds of memory usage errors.</p><p>Finally, we observe that the instrumented memory allocator and thememory leak detector both work over the entire SQLite test suite and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -