3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # This file contains test cases focused on the two memory-management APIs,
13 # sqlite3_soft_heap_limit() and sqlite3_release_memory().
15 # Prior to version 3.6.2, calling sqlite3_release_memory() or exceeding
16 # the configured soft heap limit could cause sqlite to upgrade database
17 # locks and flush dirty pages to the file system. As of 3.6.2, this is
18 # no longer the case. In version 3.6.2, sqlite3_release_memory() only
19 # reclaims clean pages. This test file has been updated accordingly.
21 # $Id: malloc5.test,v 1.22 2009/04/11 19:09:54 drh Exp $
23 set testdir [file dirname $argv0]
24 source $testdir/tester.tcl
25 source $testdir/malloc_common.tcl
28 # Only run these tests if memory debugging is turned on.
31 puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..."
36 # Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
37 ifcapable !memorymanage {
42 sqlite3_soft_heap_limit 0
46 # Simplest possible test. Call sqlite3_release_memory when there is exactly
47 # one unused page in a single pager cache. The page cannot be freed, as
48 # it is dirty. So sqlite3_release_memory() returns 0.
51 PRAGMA auto_vacuum=OFF;
53 CREATE TABLE abc(a, b, c);
55 sqlite3_release_memory
59 # Test that the transaction started in the above test is still active.
60 # The lock on the database file should not have been upgraded (this was
61 # not the case before version 3.6.2).
64 execsql { SELECT * FROM sqlite_master } db2
67 # Call [sqlite3_release_memory] when there is exactly one unused page
68 # in the cache belonging to db2.
70 set ::pgalloc [sqlite3_release_memory]
75 # Commit the transaction and open a new one. Read 1 page into the cache.
76 # Because the page is not dirty, it is eligible for collection even
77 # before the transaction is concluded.
84 sqlite3_release_memory
88 # Conclude the transaction opened in the previous [do_test] block. This
89 # causes another page (page 1) to become eligible for recycling.
92 sqlite3_release_memory
96 # Manipulate the cache so that it contains two unused pages. One requires
97 # a journal-sync to free, the other does not.
102 CREATE TABLE def(d, e, f);
104 sqlite3_release_memory 500
107 do_test malloc5-1.7 {
108 # Database should not be locked this time.
110 catchsql { SELECT * FROM abc } db2
112 do_test malloc5-1.8 {
113 # Try to release another block of memory. This will fail as the only
114 # pages currently in the cache are dirty (page 3) or pinned (page 1).
116 sqlite3_release_memory 500
118 do_test malloc5-1.8 {
119 # Database is still not locked.
122 catchsql { SELECT * FROM abc } db2
124 do_test malloc5-1.9 {
130 do_test malloc5-2.1 {
131 # Put some data in tables abc and def. Both tables are still wholly
132 # contained within their root pages.
134 INSERT INTO abc VALUES(1, 2, 3);
135 INSERT INTO abc VALUES(4, 5, 6);
136 INSERT INTO def VALUES(7, 8, 9);
137 INSERT INTO def VALUES(10,11,12);
140 do_test malloc5-2.2 {
141 # Load the root-page for table def into the cache. Then query table abc.
142 # Halfway through the query call sqlite3_release_memory(). The goal of this
143 # test is to make sure we don't free pages that are in use (specifically,
144 # the root of table abc).
145 sqlite3_release_memory
152 db eval {SELECT * FROM abc} {
153 incr nRelease [sqlite3_release_memory]
154 lappend data $a $b $c
160 } [list $pgalloc [list 1 2 3 4 5 6]]
162 do_test malloc5-3.1 {
163 # Simple test to show that if two pagers are opened from within this
164 # thread, memory is freed from both when sqlite3_release_memory() is
171 SELECT * FROM sqlite_master;
175 sqlite3_release_memory
176 } [expr $::pgalloc * 2]
177 do_test malloc5-3.2 {
179 [execsql {SELECT * FROM abc; COMMIT}] \
180 [execsql {SELECT * FROM def; COMMIT} db2]
181 } {1 2 3 4 5 6 7 8 9 10 11 12}
184 puts "Highwater mark: [sqlite3_memory_highwater]"
186 # The following two test cases each execute a transaction in which
187 # 10000 rows are inserted into table abc. The first test case is used
188 # to ensure that more than 1MB of dynamic memory is used to perform
191 # The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
192 # and tests to see that this limit is not exceeded at any point during
193 # transaction execution.
195 # Before executing malloc5-4.* we save the value of the current soft heap
196 # limit in variable ::soft_limit. The original value is restored after
199 set ::soft_limit [sqlite3_soft_heap_limit -1]
200 execsql {PRAGMA cache_size=2000}
201 do_test malloc5-4.1 {
203 execsql {DELETE FROM abc;}
204 for {set i 0} {$i < 10000} {incr i} {
205 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
209 sqlite3_release_memory
210 sqlite3_memory_highwater 1
211 execsql {SELECT * FROM abc}
212 set nMaxBytes [sqlite3_memory_highwater 1]
213 puts -nonewline " (Highwater mark: $nMaxBytes) "
214 expr $nMaxBytes > 1000000
216 do_test malloc5-4.2 {
218 sqlite3_release_memory
219 sqlite3_soft_heap_limit 100000
220 sqlite3_memory_highwater 1
221 execsql {SELECT * FROM abc}
222 set nMaxBytes [sqlite3_memory_highwater 1]
223 puts -nonewline " (Highwater mark: $nMaxBytes) "
224 expr $nMaxBytes <= 110000
226 do_test malloc5-4.3 {
227 # Check that the content of table abc is at least roughly as expected.
229 SELECT count(*), sum(a), sum(b) FROM abc;
231 } [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]
233 # Restore the soft heap limit.
234 sqlite3_soft_heap_limit $::soft_limit
236 # Test that there are no problems calling sqlite3_release_memory when
237 # there are open in-memory databases.
239 # At one point these tests would cause a seg-fault.
241 do_test malloc5-5.1 {
246 CREATE TABLE abc(a, b, c);
247 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
248 INSERT INTO abc SELECT * FROM abc;
249 INSERT INTO abc SELECT * FROM abc;
250 INSERT INTO abc SELECT * FROM abc;
251 INSERT INTO abc SELECT * FROM abc;
252 INSERT INTO abc SELECT * FROM abc;
253 INSERT INTO abc SELECT * FROM abc;
254 INSERT INTO abc SELECT * FROM abc;
256 sqlite3_release_memory
258 do_test malloc5-5.2 {
259 sqlite3_soft_heap_limit 5000
262 PRAGMA temp_store = memory;
263 SELECT * FROM abc ORDER BY a;
267 sqlite3_soft_heap_limit $::soft_limit
269 #-------------------------------------------------------------------------
270 # The following test cases (malloc5-6.*) test the new global LRU list
271 # used to determine the pages to recycle when sqlite3_release_memory is
272 # called and there is more than one pager open.
275 set bt [btree_from_db $db]
276 array set stats [btree_pager_stats $bt]
280 forcedelete test.db test.db-journal test2.db test2.db-journal
282 # This block of test-cases (malloc5-6.1.*) prepares two database files
283 # for the subsequent tests.
284 do_test malloc5-6.1.1 {
287 PRAGMA page_size=1024;
288 PRAGMA default_cache_size=10;
291 PRAGMA temp_store = memory;
293 CREATE TABLE abc(a PRIMARY KEY, b, c);
294 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
296 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
298 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
300 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
302 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
304 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
306 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
309 forcecopy test.db test2.db
312 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
314 do_test malloc5-6.1.2 {
315 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
318 do_test malloc5-6.2.1 {
319 execsql {SELECT * FROM abc} db2
320 execsql {SELECT * FROM abc} db
321 expr [nPage db] + [nPage db2]
324 do_test malloc5-6.2.2 {
325 # If we now try to reclaim some memory, it should come from the db2 cache.
326 sqlite3_release_memory 3000
327 expr [nPage db] + [nPage db2]
329 do_test malloc5-6.2.3 {
330 # Access the db2 cache again, so that all the db2 pages have been used
331 # more recently than all the db pages. Then try to reclaim 3000 bytes.
332 # This time, 3 pages should be pulled from the db cache.
333 execsql { SELECT * FROM abc } db2
334 sqlite3_release_memory 3000
335 expr [nPage db] + [nPage db2]
338 do_test malloc5-6.3.1 {
339 # Now open a transaction and update 2 pages in the db2 cache. Then
340 # do a SELECT on the db cache so that all the db pages are more recently
341 # used than the db2 pages. When we try to free memory, SQLite should
342 # free the non-dirty db2 pages, then the db pages, then finally use
343 # sync() to free up the dirty db2 pages. The only page that cannot be
344 # freed is page1 of db2. Because there is an open transaction, the
345 # btree layer holds a reference to page 1 in the db2 cache.
348 UPDATE abc SET c = randstr(100,100)
349 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
351 execsql { SELECT * FROM abc } db
352 expr [nPage db] + [nPage db2]
354 do_test malloc5-6.3.2 {
355 # Try to release 7700 bytes. This should release all the
356 # non-dirty pages held by db2.
357 sqlite3_release_memory [expr 7*1132]
358 list [nPage db] [nPage db2]
360 do_test malloc5-6.3.3 {
361 # Try to release another 1000 bytes. This should come fromt the db
362 # cache, since all three pages held by db2 are either in-use or diry.
363 sqlite3_release_memory 1000
364 list [nPage db] [nPage db2]
366 do_test malloc5-6.3.4 {
367 # Now release 9900 more (about 9 pages worth). This should expunge
368 # the rest of the db cache. But the db2 cache remains intact, because
369 # SQLite tries to avoid calling sync().
370 if {$::tcl_platform(wordSize)==8} {
371 sqlite3_release_memory 10500
373 sqlite3_release_memory 9900
375 list [nPage db] [nPage db2]
377 do_test malloc5-6.3.5 {
378 # But if we are really insistent, SQLite will consent to call sync()
379 # if there is no other option. UPDATE: As of 3.6.2, SQLite will not
380 # call sync() in this scenario. So no further memory can be reclaimed.
381 sqlite3_release_memory 1000
382 list [nPage db] [nPage db2]
384 do_test malloc5-6.3.6 {
385 # The referenced page (page 1 of the db2 cache) will not be freed no
386 # matter how much memory we ask for:
387 sqlite3_release_memory 31459
388 list [nPage db] [nPage db2]
393 sqlite3_soft_heap_limit $::soft_limit