Memory management - forcing a process to free it's memory
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Memory management - forcing a process to free it's memory

  1. #1
    Join Date
    Dec 2005
    Posts
    6

    Question 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...

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  3. #3
    Join Date
    Dec 2005
    Posts
    6

    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?

  4. #4
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Memory management - forcing a process to free it's memory

    Quote 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...
    			}

  5. #5
    Join Date
    Dec 2005
    Posts
    6

    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?

  6. #6
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Memory management - forcing a process to free it's memory

    Quote 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.

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Memory management - forcing a process to free it's memory

    Quote 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.

    Quote 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center