问题
I am using pyodbc (3.0.7) to retrieve data from a SQL Server database. My OS is windows 7 and python is 2.7.7 64 bit. I am running into memory usage issues and I was wondering if there are any solutions.
Each time I run my program, the memory usage of the sqlservr.exe process keeps increasing (based on running Tasklist at the command prompt). Here's my code.
1.Connect to database
import pyodbc
cnxnstring ="DRIVER={SQL Server};Server=servername;Database=dbname; \
User Id=uid; Password=pwd;"
cnxn = pyodbc.connect(cnxnstring)
dbcursor = cnxn.cursor()
2. Retrieve data from database and store in dictionary
strsql = 'SELECT CELLID,MEAN FROM TABLE_1'
dbcursor.execute(strsql)
dict1= {}
for line in dbcursor: dict1.update({line.CELLID:line.MEAN})
3. Do something with dictionary - code not relevant here
4. Housekeeping
dbcursor.close()
cnxn.close()
Even though I close the connection to the SQL Server database, Windows does not release the associated memory. Memory usage keeps building up over time and I am forced to restart my computer. What can I do to solve this problem ?
Edit : Temporary Workaround
I found a temporary workaround for my problem. I went into Windows Services and stopped the SQLSERVER process to release memory and restarted it again. I guess I could do this programmatically as well but I will leave that for another day.
回答1:
By default, SQL Server will allocate as much memory as it can for the buffer pool. This is great for performance, allowing data to be accessed from (fast) memory instead of (slow) disk. For development workstations, this can become a problem when you have other applications competing for resources.
To resolve, configure a maximum amount of memory that SQL Server should allocate. The amount you set depends on the amount of memory installed and the type/number of applications you are using. As a very general recommendation for a developer workstation with 8GB installed, I would set the SQL Server maximum to 2048MB.
来源:https://stackoverflow.com/questions/24766178/manage-pyodbc-memory-usage