-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Potential memory leak on Sql.Db.Close() #1319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm new to Go, but isn't it better to put writeDB.Close() and readDB.close(0 in defer for this scenario? Correct me if I'm wrong, but for each error checking, if the error is not nil, the function will return an empty value. Other codes below whichever failed error checking will not executed after that. So if one of the error checking fails, the function will just finish without even touching the writeDB.close() and readDB.close(). By using defer it make sure that whatever happens, the database will be closed before the function is close. |
absolutely! tbf it was the original version of my code, i wanted to be sure that |
Well, firstly and most obviously you are eating errors and returning early. This isn't how you should handle errors in Go. your Close calls should be done in deferrals immediately after checking whether Open returned an error or not. As far as GC/memory leak goes, does your onDbFunc make use of goroutines perhaps? i would get rid of the callback functionality and run some tests just to confirm. Maybe something there holding onto a reference. |
Issue #1328 also faces a memory leak problem. It's probably similar to what you've faced. |
@jmcdonagh as i said earlier i was facing the same issue when i was using no goroutines at all in the callback, just classic sql execution mainly |
@yafethtb it could be the case indeed :/ |
sqlite3 version:
github.com/mattn/go-sqlite3 v1.14.22
Hello, i would like to know if i'm doing something wrong here... It seems that
sql.Db.Close()
does not release allocated memory when called.In fact, i suspect it to cause a memory leak.
I have a project that is a
net/http
api that exposes aGET /instances/{instance}/jobs/{jobID}/status
route.instance
is a hash string that i get the name of an sqlite database file from.jobID
is theID
of ajob
in the given database.My service will take the database
{instance}.sqlite
, opens it and query the status of the job{jobID}
, Afterward it will close the given database.Here's a code sample that shows how i open and close both of my databases.
The problem is that when i close my
Sql.Db
handles, the allocated memory is not released, even after some time. I actually work on a lot of different sql databases which causes my service to crash from OOM error eventually.I'm sure my pattern is not efficient, it is a proof of concept. But i would expect resources to be released when i close my handles, or at least be garbage collected after some time, which does not happen.
Could someone explain to me what i am doing wrong please ?
Thank you for your time.
The text was updated successfully, but these errors were encountered: