1

I'm writing an application that spawns and kills a Chrome Browser. While I'm killing the process programatically (same effect as killing process through Windows Task Manager), it's been suggested that this may also result in memory leak--ie from elements such as kernel resources that were not properly associated w/ the originating process.

Is it possible to for an application to leak memory or otherwise have irreclaimable memory after the Process has been killed?

3
  • Not in a real OS. Embedded stuff, maybe, but not in an OS like Windows or Linux.
    – cHao
    May 2, 2012 at 21:38
  • That would actually be the OS leaking memory. Yes, that is possible. But very unlikely
    – sehe
    May 2, 2012 at 22:17
  • Note that Chrome's application data folder might become corrupted if it is in the middle of updating a file when you kill it. May 3, 2012 at 2:50

4 Answers 4

2

This may happen, but when it does, it's never a bug in the program. It is always caused by a bug/error in either the windows kernel code, or some kernel driver. Also it is very unlikely that you will even encounter such behaviour on a standard installation of Windows.

1

Killing a process in task manager directly could result in memory leak. An evidence is keeping killing "explorer.exe" will slow down the entire OS.

Each Chrome window has a GUI process and a background process for each active tab. Background processes is reponsible for fetching page content, sending request / posting data, and hold fewer kernel object handles than GUI processes. Any programming framework will have at least two methods to close an active process. For example, in .NET, Process class has these two methods:

  1. CloseMainWindow()
    • Sends a WM_QUIT message to the main window message loop to request to close the process. This gives the program a chance to reinvoke its child window and its kernel objects.
  2. Kill()
    • Forces a termination of a process, same as killing the process in task manager. This is the only way to close background process. but when this applies to GUI processes, this doesn't trigger all GUI/kernel events to release resources. Kernel objects include GDI objects, file/printer handles, database/network connection contexts, etc. Releasing these resources take some time while tracking down the entire memory linked list/map to deallocate kernel objects' resources. Even .NET/java has its garbage collector to release "managed" memory for sure, kernel objects' unmanaged resources are not always covered.

The following is a test app in .NET C#. We can compare CloseMainWindow() to Kill() using a Visual Studio add-on: Memory Profiler. In almost all cases, Kill() is faster but has less memory released by checking "root references" and "instance references" in real-time memory graph.

private Process _chromeProcess = new Process();

private void bntCreate_Click(object sender, EventArgs e)
{
    string chromePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
        @"Google\Chrome\Application\chrome.exe");

    _chromeProcess.StartInfo = new ProcessStartInfo(chromePath, @"-app=http://localhost:(a local Web site)");
    _chromeProcess.Start();
}

private void btnKill_Click(object sender, EventArgs e)
{
    _chromeProcess.Kill();
    _chromeProcess.WaitForExit();
    _chromeProcess.Close();
}

private void btnClose_Click(object sender, EventArgs e)
{
    _chromeProcess.CloseMainWindow();
    _chromeProcess.WaitForExit();
    _chromeProcess.Close();
}
3
  • 1
    Killing a process can't result in a memory leak unless there is a bug in the operating system (or an installed device driver). I'm not sure what you mean by "keeping killing explorer.exe" but any issue is unlikely to be due to a memory leak (although admittedly explorer.exe is a special case). May 6, 2012 at 4:25
  • As for your experiment - what are you measuring memory with? What is a "root reference" and what is an "instance reference"? Those terms don't appear in Win32 memory management AFAIK. It sounds as if you are measuring the memory used by your own .NET process, but that has nothing to do with the memory Chrome is using. May 6, 2012 at 4:29
  • "when this applies to GUI processes, this doesn't trigger all GUI/kernel events to release resources" is not true. Killing a process definitely releases all GUI and kernel resources associated with that process. Note that from the kernel's point of view, exiting a process and terminating a process are the same thing: ExitProcess() calls TerminateProcess(GetCurrentProcess()). May 6, 2012 at 4:32
1

A program might start more than one process. Killing the parent process doesn't in itself guarantee that the child processes will terminate. This could look like a memory leak. As far as the operating system is concerned, all the allocated memory remains associated with a running process, it's just that you might not expect all those processes still to be running.

As far as Chrome is concerned, it does spawn lots of child processes. I think it makes sure that they clean themselves up when the parent process is unexpectedly terminated, but I couldn't be sure.

0

If a program places a large amount of memory on the clipboard it may look like a memory leak, but the clipboard is still in charge of the memory and will release it when something else copies something new.

That's the only case I can think of for memory outliving the lifetime of a program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.