Databasereader Not enough storage not solved
corradin opened this issue · 5 comments
I downloaded the latest version and although some of the code has been changed, when I create a new reader on every client request it still throws this exception. I understand that you advise to use only 1 instance, but in a WEB API environment (which is stateless) it is not advisable to share instances. In a WEB API environment I would strongly suggest to move to an MS SQL Database and use Entity Framework to solve the memory issue. I will export the Maxmind csv to my SQL DB and work from there. Hopefully the GEOIP2 sees an update in the future to be more WEB API compatible.
There are many different options to cache the reader object between web requests. Setting up the reader object is expensive and it should not be done each request.
As for opening the reader many, many times as you appear to be doing, you likely would not have the problem when using the memory mode. The error you allude to appears to be a limitation of how memory mapped files are implemented in Windows.
I could look into caching thanks for that :). Changing the memory mode will cause memory to go sky high, since after each request memory is increased. I assume the Maxmind DB is loaded into the LOH (Large Object Heap) and .NET has some vulnerabilities in cleaning up the LOH.
Yeah, at the very least, using memory mode would cause some serious pressure on the garbage collector. Using one of the caching options is the best solution.
HTTP is stateless so any web app servicing these requests will need to maintain it's own state, but that being said there's no reason why you can't share services across requests. This geolocation database is just as stateless as your app.
Most services and modules used by asp.net are also implemented as single instances that live for the life of the appdomain so this is an extremely common pattern. You can just use a global static variable holding the database reader that's initialized in the application startup (and disposed in the app shutdown).
Closing as using a new reader for every request is not recommended. This particular error appears to be an artifact of memory mapping on Windows. If you must create a new reader for every request, it is recommended that you don't use the memory mapping mode.