powershell - DataReader causing SQL Server to leak available memory -


i have powershell script that's reading data local sql server express 2014 database , writing csv file. works fine, it's using entire pool of available memory sql server, , not releasing when script finished running. calling gc.collect() doesn't release memory; have restart sql server service back.

here's relevant portion of script, i've determined leak occurring:

try {     $sqlconn = new-object -typename system.data.sqlclient.sqlconnection($sqlconnstring);     $sqlconn.open();      $sqlcmd = new-object -typename system.data.sqlclient.sqlcommand;     $sqlcmd.connection = $sqlconn;     $sqlcmd.commandtype = [system.data.commandtype]::text;     $sqlcmd.commandtext = "select * $exportreadings";      #write contents csv file     $line = new-object -typename system.text.stringbuilder;     $out = new-object -typename system.io.streamwriter -argumentlist $csvfile, $false;     $out.writeline("header_row");             $reader = $sqlcmd.executereader();     $cols = $reader.visiblefieldcount;     while ($reader.read()) {         $line.clear();         ($i = 0; $i -lt $cols; $i++) {             $val = $reader.getvalue($i);             if ($val -like "*$delimiter*") {                 $line.append('"').append($val).append('"');             }             else {                 $line.append($val);             }              if ($i -ne ($cols - 1)) {                 $line.append($delimiter);             }         }         $out.writeline($line.tostring());     } } catch {     throw; } {     if ($reader) { $reader.dispose(); }     if ($out) {         $out.close();         $out.dispose();     }     $sqlcmd.dispose();     $sqlconn.dispose(); } 

if query memory usage on sql server before running script (i'm using select * sys.dm_exec_query_resource_semaphores), `available_memory_kb) of 750k. afterwards, it's around 150k, once script has terminated.

i've tried release every resource can, must missing something. ideas?

i don't think powershell issue @ all. if you're seeing sql server's memory has increased, normal. sql server use memory can, caching. memory released if else in system needs it, idea if possible bring every database memory , leave there future accesses fast.

a sysadmin’s guide microsoft sql server memory


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -