0

My program is using webbrowser in c# and if i change too much page, i get memory leak problem. I find some solution, but if I tried these solutions for example:

finally
{
GC.Collect();
GC.WaitForPendingFinalizers();

EmptyWorkingSet(GetCurrentProcess());
}

firstly, memory turns back to normal, but when i change to page or use to webbrowser, memory leak resumes from where it left off. I tried many things but I didn't find a solution.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 3
    What makes you think there's a memory leak? – John Saunders Jun 25 '14 at 01:14
  • Probably it is about IE, i tried only web browser aplication on other pc and I get same memory leak problem. For example memory usage only 67 mb when program begin, if i change many page, memory usage over 920 MB and if i clear memory it decrease almost 67 MB again. But I enter only one page again program jump to 920 MB again. – user3773215 Jun 25 '14 at 01:43
  • 1
    Why do you call that a memory "leak"? It's a memory leak only if memory usage keeps going up. Also, how are you measuring memory usage? Probably by using Task Manager? If so, then you're looking at _virtual_ memory, not _physical_ memory. Not the same thing. – John Saunders Jun 25 '14 at 03:45

1 Answers1

0

This code will free the memory similar to garbage collect. I found it useful to add it to a timer to automate the process.

    [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

    [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    internal static extern IntPtr GetCurrentProcess();

    private void button1_Click(object sender, EventArgs e)
    {
        IntPtr pHandle = GetCurrentProcess();
        SetProcessWorkingSetSize(pHandle, -1, -1);
    }
Msegling
  • 365
  • 3
  • 12
  • This code will write the memory to the page file, which can eventually also get full - see this [post](http://stackoverflow.com/questions/904478/how-to-fix-the-memory-leak-in-ie-webbrowser-control) and Azuvector's comment there. – BornToCode Aug 29 '16 at 15:09