Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# simple app create enormous number in page faults. Why?

Tags:

c#

windows

c#-3.0

Have simple C# console app which imports text data into SQL.

It takes around 300K in memory and 80% in CPU. There are 2Gb RAM available at any time and yet the Page Fault shows 500K.

The app is 32 bit and OS is either W2000 or XP 32 bit and .NET 3.5

Anyone can explain what could be the problem and how can I investigate this further?

EDIT: I am now certain that the page faults are related to the disk I/O (read). I commented out SQL part and the pure disk read generates that high number alone.

EDIT2: There are 200 hard faults/sec and 4000 soft faults/sec on average.

I wonder if the same would appear on W2008

like image 258
Boppity Bop Avatar asked Jan 21 '23 10:01

Boppity Bop


2 Answers

First, how do you measure the memory the app is using? If you're looking at "working set" that's only the part that resides in physical memory. You should also take a look at the "VM Size" (or "Commit Size") where the actual virtual memory your process takes up.

If Windows kernel Balance Set Manager thinks that your app is inactive, or should be left behind to give other processes more power, it can decide to reduce the working set size. If working set size is smaller than what your application actually needs to work on, you could easily see a lot of page faults because it simply becomes a race between The Balance Set Manager and the application. Usually balance set manager monitors memory usage and can also decide to increase working set size accordingly. However, this might be prevented in certain circumstances like low physical free memory, high I/O (cache stress on physical memory), low process priorty, background/foreground status of the application etc.

It can simply be the behavior of .NET garbage collector due to vast amount of small memory blocks getting allocated and disposed in a very short time, causing a stress on both memory allocation and releasing. The "VM Size" could stay around the same size but behind the scenes it could be continously allocating/freeing memory, causing continous page faults.

Also know that the DLLs the process is using are also accounted for the process statistics. Not your app but one of the COM or .NET DLL you are using might be causing this behavior as well. You can deduce actual culprit by changing your application's behavior (e.g. removing DB access code and only leave object allocation code behind) to see which component is actually causing thrashing.

EDIT: About your question on GC impact on memory thrashing: The CLR actually grows the heap dynamically and gives the memory back to the OS as needed. That does not occur synchronously. GC runs behind the scenes and frees memory in large chunks to prevent hindering application performance. Say you are allocating many small objects and freeing them almost immediately. That causes many references to stay for a moment in memory before freeing. It is easy to imagine that it becomes like a head-to-head race between the garbage collector and the memory allocating code. While GC eventually catches up, the required new memory must be satisified from a "new memory", not the old one because old one is not freed up yet. Since actual memory we are working on stays around the same, balance set manager may not think of giving our process more memory because we're on the edge, always around the same physical memory size but constantly need "newly allocated memory" not "more memory", therefore page faults.

like image 142
Sedat Kapanoglu Avatar answered Jan 30 '23 07:01

Sedat Kapanoglu


Page faults are normal. Memory gets swapped out and when you next access it that's a page fault and the system brings it back. This is by design.

I've got an app running on my machine right now with 500 million page faults. There's nothing to worry about!

like image 37
David Heffernan Avatar answered Jan 30 '23 06:01

David Heffernan