当前位置:网站首页>[SQLite3] memory debugging
[SQLite3] memory debugging
2022-06-12 10:09:00 【Lindo】
Sqlite3 Memory mode
- Basically Sliqte3 Provided interface , As long as it's operated correctly , There is no memory leak . It has a strict memory theory , And implementation mechanism . The main discussion here is , How to perform memory mode 、 confirm .
Sqlite3 Dynamic memory
- see sqllite3 The size of the dynamic memory currently held (byte). This interface is accessed through the SQLITE_STATUS_MEMORY_USED Status value implementation . The general principle is ,sqlite3 When dynamic memory is requested or released , Will SQLITE_STATUS_MEMORY_USED Add or subtract the allocated value from the corresponding status value bytpe The operation of .
// Interface
sqlite3_int64 sqlite3_memory_used(void);
// Interface implementation
// The interface will return sqlite3 The size of dynamically allocated memory held .
sqlite3_int64 sqlite3_memory_used(void){
int n, mx;
sqlite3_int64 res;
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
return res;
}
// sqlite3 Internal implementation logic
/* ** Free memory previously obtained from sqlite3Malloc(). */
void sqlite3_free(void *p){
// Omit
// Record the number of bytes of memory released ( negative )
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, - sqlite3MallocSize(p));
}
/* ** Do a memory allocation with statistics and alarms. Assume the ** lock is already held. */
static int mallocWithAlarm(int n, void **pp){
// Record the requested memory szie( Positive numbers )
nFull = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
}
- obtain sqlite3 Internal state interface , first in The parameter indicates the state type , the second out The parameter represents the current value of the status , Third output The parameter is the highest value of the status record , The fourth parameter is ture Indicates that the highest value of the record is cleared after the function .
int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
- Available status values
// Current memory
#define SQLITE_STATUS_MEMORY_USED 0
// Page memory
#define SQLITE_STATUS_PAGECACHE_USED 1
#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
#define SQLITE_STATUS_SCRATCH_USED 3
#define SQLITE_STATUS_SCRATCH_OVERFLOW 4
#define SQLITE_STATUS_MALLOC_SIZE 5
#define SQLITE_STATUS_PARSER_STACK 6
#define SQLITE_STATUS_PAGECACHE_SIZE 7
#define SQLITE_STATUS_SCRATCH_SIZE 8
// Executed malloc The number of (malloc when +1,free when -1)
#define SQLITE_STATUS_MALLOC_COUNT 9
- modify sqlite Code , Check for memory leaks . modify sqlite3StatusAdd function , Let it execute , Output of allocated memory size. At the same time to modify sqlite3MemSize and sqlite3MemFree function , Let it output the address of the pointer . example :
// Output memory increases / Released size
void sqlite3StatusAdd(int op, int N){
if (SQLITE_STATUS_MEMORY_USED == op) {
printf("sqlite3StatusAdd memory change [%d]", N);
}
// ....
}
// In general , Memory allocation 、 Release will call this function , perhaps size, And then use sqlite3StatusAdd Record . So it can be added to this function .
static int sqlite3MemSize(void *pPrior){
#ifdef SQLITE_MALLOCSIZE
return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
#else
sqlite3_int64 *p;
if( pPrior==0 ) return 0;
p = (sqlite3_int64*)pPrior;
p--;
printf("sqlite3MemSize buffer addr[%p]", p);
return (int)p[0];
#endif
}
// Look at the address of the freed memory .
static void sqlite3MemFree(void *pPrior){
#ifdef SQLITE_MALLOCSIZE
SQLITE_FREE(pPrior);
#else
sqlite3_int64 *p = (sqlite3_int64*)pPrior;
assert( pPrior!=0 );
p--;
printf("sqlite3MemFree addr[%p]", p);
SQLITE_FREE(p);
#endif
}
In the source code , Append output information , Can be observed more directly sqlite Allocated memory information .
in addition , You can also use varglind Perform memory mode .
About Sqlite3 Memory can not be released
- Recently I met a Sqlite3 Memory can not be released , adopt sqlite3_finalize Post release function , The memory of the viewing process is still not reduced . On this question , After analysis , Finally found sqlite3 For all dynamically requested memory , They all called. free operation . The real cause of the problem is , Running the process On the system , about malloc Of memory ,free It will not be released immediately after ( About the cause and solution , There are some articles on the Internet , Can own Baidu ).
边栏推荐
- 总有一根阴线(上影线)会阻止多军前进的脚步,总有一个阳线(下影线)会阻挡空军肆虐的轰炸
- SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
- JVM (IV) Class file structure (complete parsing of bytecode attached)
- 7-4 网红点打卡攻略(dfs)
- Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
- UE4_以现成资源探索创建背景场景的方法
- C 语言仅凭自学能到什么高度?
- markdown_图片并排的方案
- In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion
- MySQL VI Database lock
猜你喜欢

markdown_ Picture side by side scheme

基于SSM实现水果商城批发平台
![Data processing and visualization of machine learning [iris data classification | feature attribute comparison]](/img/a3/1d181b9dd5268e13cc276a78862d1f.png)
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]

How to implement Web3.0 and digital fashion?

UE4_ Explore the method of creating background scenes with ready-made resources

In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion

Code implementation of hash table based on linear detection

【clickhouse专栏】基础数据类型说明

Auto. JS learning note 4: after autojs is packaged, most Huawei and other big brand mobile phones cannot be installed? This problem can be solved by using the simulator to remotely sign and package in
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
随机推荐
[Wayland] Wayland agreement description
UE4_ Explore the method of creating background scenes with ready-made resources
2021-02-12
MySQL 4 Database table storage structure & tablespace
Introduction to IOT
Auto. JS learning notes 5:ui interface foundation of autojs Chapter 1
Using C language code to realize factory LCD RGB test program
001: what is a data lake?
【系统分析师之路】第十八章 复盘系统安全分析与设计
FPGA VGA display based on de2-115 platform
日本经济泡沫与房价泡沫
Essentials reading notes
Create simple windowing programs using Visual Studio 2017
tp6调试(trace)
用于图像处理的高性能计算框架
【ParquetEncodingException: empty fields are illegal, the field should be ommited completely instead
哈希表的线性探测法代码实现
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)
Research progress of DNA digital information storage
Introduction to applet