0

I am experiencing a weird memory problem. The process in which my C++ application executes consumes both virtual and physical memory as it time progresses.

When I shut down the process, whatever virtual memory that it consumes is returned to the system. However, most of the physical memory that is consumed is never returned to the system. I can view this in Task Manager.

Any memory that my application dynamically allocates should be returned to the system when its process terminates.

The only suspect computations that my application is doing is repeatedly calling FindFirstFile/FindNextFile to examine some existing files in the file system. The operation examines about 10 million files in total. In this case, I believe that I am correctly closing the file handle returned by FindFirstFile.

Can anyone speculate as to why the physical memory is not being returned to the system?

rohitsan
  • 1,001
  • 8
  • 31
  • Can you post your code where you think you might have a memory leak? You can also use valgrind to analyze your code. – NathanOliver Jan 21 '15 at 19:05
  • You have a memory leak that much is certain. Without seeing any code there is no way we can help you beyond saying you should free any memory you allocate ... – Goz Jan 21 '15 at 19:05
  • possible duplicate of [Visual C++ - Memory Leak Detection](http://stackoverflow.com/questions/2820223/visual-c-memory-leak-detection) – dewaffled Jan 21 '15 at 19:06
  • @frymode: From the question title, you'd think that, but there's a big difference if you actually read the whole question. – Ben Voigt Jan 21 '15 at 19:07
  • "The usage of physical memory is causing performance problems with my application." Very doubtful. Can you substantiate that with some numbers? (My system has x GB of physical RAM, I'm using Y tool to view "Committed Pages", when this number goes above Z my application begins spending r milliseconds to perform action q instead of t milliseconds) – Ben Voigt Jan 21 '15 at 19:11
  • You are right - that comment is doubtful so I deleted it. Thanks for your insight. – rohitsan Jan 21 '15 at 19:13
  • @NathanOliver valgrind is not available on Windows – inetknght Jan 21 '15 at 19:16
  • @inetknght Valgrind + Wine can be made to work with some effort. – NathanOliver Jan 21 '15 at 19:20

3 Answers3

3

The physical memory is always under the control of the OS. Whatever tool you are using to measure physical memory usage is misleading you.

Possibly the disk cache is growing, possibly the low priority background thread that zeros free pages is not keeping up and your tool treats "free but dirty" differently from "free and zeroed for reuse".

It's also possible that your application is triggering a memory leak in some driver or another process that hasn't exited yet.

In no case is your application continuing to own physical memory after it terminates.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

Don't spend too much effort figuring out what is going on with physical memory, the operating system should take care of it. You'll usually just go in circles trying to figure it out unless you have good understanding of what is going on in the OS kernel. I suspect it's leaving something in disk cache and it will release it when needs free pages.

1

The OS has a goal of using as much physical memory as it can - anything that isn't being used is wasted. The paging system will ensure that any application that needs physical memory will get it, by swapping something else out if necessary.

One of the ways it uses memory is for caching files from disk. A directory is also a file, so it gets cached too as you read through it.

I've always felt personally that Windows does too much disk caching. I'm much more likely to switch to a program that I haven't used in a while than I am to access a file that I haven't used in a while.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622