iuradz
iuradz

Reputation: 1168

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?

Upvotes: 1

Views: 1898

Answers (1)

matteeyah
matteeyah

Reputation: 875

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.

Upvotes: 2

Related Questions