Documente Academic
Documente Profesional
Documente Cultură
in
SQLite Core
failover
SQLite Core
failover
X
Gets back N+X bytes
Heap
External Fragmentation
Heap
External Fragmentation
Heap
Minimum Required Heap Size
● n: max size of any memory allocation
● M: max memory outstanding
● H: heap size to avoid breakdown
H = M(1 + 0.5*log2(n)) – n + 1
size_t N = 1000 /* Size of hashtab/buffer/cache */;
void *p = sqlite3_malloc64(N);
N = sqlite3_msize(p);
/* Use all N bytes of p ... */
sqlite3_status64()
● General purpose memory used
● Largest GP allocation size
● Page cache slots used
● Page cache overflows to GP allocator
● Largest page cache allocation size
● Scratch memory used
● Scratch memory overflows to GP allocator
● Largest scratch memory allocation size
.stats on
Memory Used: 189184 (max 192512) bytes
Number of Outstanding Allocations: 1070 (max 1128)
Number of Pcache Overflow Bytes: 46824 (max 46824) bytes
Number of Scratch Overflow Bytes: 0 (max 0) bytes
Largest Allocation: 64000 bytes
Largest Pcache Allocation: 1272 bytes
Largest Scratch Allocation: 0 bytes
Lookaside Slots Used: 3 (max 81)
Successful lookaside attempts: 399
Lookaside failures due to size: 157
Lookaside failures due to OOM: 0
Pager Heap Usage: 47192 bytes
Page cache hits: 30
Page cache misses: 36
Page cache writes: 0
Schema Heap Usage: 69592 bytes
Statement Heap/Lookaside Usage: 1808 bytes
Fullscan Steps: 0
Sort Operations: 0
Autoindex Inserts: 0
Virtual Machine Steps: 10
Memory Sizing
● Rule of thumb: more memory = faster
● 50 to 100 cache pages per database
connection
● 2 to 50 kB per prepared statement
● Schema parse is held in memory – avoid overly
complex schemas
Custom Page Cache
● Dynamically sized according to system load?
● Discard pages when under memory pressure?
● Setup using sqlite3_config()
struct sqlite3_pcache_methods2 {
int iVersion;
void *pArg;
int (*xInit)(void*);
void (*xShutdown)(void*);
sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
void (*xCachesize)(sqlite3_pcache*, int nCachesize);
int (*xPagecount)(sqlite3_pcache*);
sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
unsigned oldKey, unsigned newKey);
void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
void (*xDestroy)(sqlite3_pcache*);
void (*xShrink)(sqlite3_pcache*);
};
PRAGMA temp_store
● PRAGMA temp_store=FILE;
– Temporary data stored on disk
– Less memory used, but slower
● PRAGMA temp_store=MEMORY;
– Temporary data stored in RAM
– Faster, but uses more memory
● Default behavior determined by
SQLITE_TEMP_STORE compile-time option