-
December 17th, 2005, 07:44 PM
#1
Memory management - forcing a process to free it's memory
Hi,
I'm writing a program that interacts with sql server. My problam is that sql server (running under the sqlservr.exe process) does not free it's memory never, which causes the memory amount he holds increase significatly after a short time, and the system to slow down. Programs like cacheman or FreeRam forces such processes to free their memory. However, this is not a solution I can offer my client.
How can I force a given process to free unused memory? What are the API involved and how can I use them?
Hope someone can help me...
-
December 17th, 2005, 08:14 PM
#2
Re: Memory management - forcing a process to free it's memory
Maybe you can use the PSAPIs EmptyWorkingSet:
Code:
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace MyNamespace
{
class MyProgram
{
[DllImport("psapi.dll")]
public static extern bool EmptyWorkingSet(IntPtr hProcess);
static void Main(string[] args)
{
// get handle to a process
Process pProcess = Process.GetCurrentProcess();
// empty as much as possible of its working set
bool bRes = EmptyWorkingSet(pProcess.Handle);
if (!bRes)
{
// EmptyWorkingSet failed...
}
}
}
}
- petter
-
December 18th, 2005, 04:41 AM
#3
Re: Memory management - forcing a process to free it's memory
Thanks, that seemed to have done the job perfectly.
Does this effect performance? I mean, after this call does the process free (or page) all its memory or just unused memory?
-
December 18th, 2005, 09:34 AM
#4
Re: Memory management - forcing a process to free it's memory
Originally Posted by wildfrog
bool bRes = EmptyWorkingSet(pProcess.Handle);
if (!bRes)
{
// EmptyWorkingSet failed...
}
I am not sure if EmptyWorkingSet executes asynchronously but if so it doesn't give a correct result,I think it is better to use a while cycle
Code:
bool bRes = EmptyWorkingSet(pProcess.Handle);
int c=0;
while(!bRes && c<1000000)
{
bRes = EmptyWorkingSet(pProcess.Handle);
c++;
}
if (!bRes)
{
// EmptyWorkingSet failed...
}
-
December 18th, 2005, 09:44 AM
#5
Re: Memory management - forcing a process to free it's memory
Does anybody know what the side effects of EmptyWorkingSet are?
Does it free only un-used memory while preserving needed memory for the process, or will a call to EmptyWorkingSet cause many page faults later on?
-
December 18th, 2005, 10:44 AM
#6
Re: Memory management - forcing a process to free it's memory
Originally Posted by oren_mh
Does anybody know what the side effects of EmptyWorkingSet are?
Does it free only un-used memory while preserving needed memory for the process, or will a call to EmptyWorkingSet cause many page faults later on?
workingset is a place for the pages which the process would deal with them in an interval more.
removing pages from the working set would cause more page faults in return.
-
December 18th, 2005, 11:20 AM
#7
Re: Memory management - forcing a process to free it's memory
Originally Posted by mehdi62b
I am not sure if EmptyWorkingSet executes asynchronously but if so it doesn't give a correct result,I think it is better to use a while cycle
I couldn't find any documentation indicating that EmptyWorkingSet (or SetProcessWorkingSetSize) could end asynchronously.
Originally Posted by oren_mh
Does it free only un-used memory while preserving needed memory for the process, or will a call to EmptyWorkingSet cause many page faults later on?
The working set of an application is not guaranteed to be reserved. When the application is idle or the the memory runs low the OS will eventually reclaim/reduce the working set of an application. So, calling the EmptyWorkingSet/SetProcessWorkingSetSize is just a way to clean up before the OS does it.
- petter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|