简体   繁体   中英

Memory leak in SQLite for C#?

I'm using sqlite in C#. To test the performance when the size of database goes large, I write a program to generate random data, which ran out of memory. I found I have created a new IDbCommand instance by

IDbCommand cmd = dbConnection.CreateCommand() ;

method for every new insert, and that's where the memory leaks. And if I call `

cmd.Dispose();`

after executing the SQLCommand, everything is OK. Usually, there won't be large number of operations, so I don't mind the time lose of creating new Command instance for every operation. But if the memory won't be disposed, that's a problem.

Is that my duty to call cmd.Dispose() or it's a bug of sqlite-net?

It's good practice to dispose everything that implements IDisposable .

You could use the using statement to do this:

using (IDbCommand cmd = dbConnection.CreateCommand())
{
    // Your code here
}

Please note that cmd goes out of scope outside of the using statement block. The using statement disposes your IDisposable when executes goes out of that block. It also disposes everything in case of an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM