Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Anyone have any ideas if there is a simple way to measure how much memory a certain part of application is consuming?
For example I got an application that load text files, processes these and takes another one. I need to measure how much memory the single file processing took.
I know I could stop the GC and take the memory snapshots, but this would affect performance and nobody recommends doing that for such purpose.

Any other ideas for this?

by the way- I am not interested in any profiling tool since I need to log this memory usage into db.
And there are many such peaces running everywhere.

thanks!

What I have tried:

did some googlig.
read several articles on MSDN
Posted
Updated 13-Mar-17 1:09am

1 solution

Have you tried using the below method ?

GC.GetTotalMemory(true)


This you can use at start / end of the method and get the difference.

Regards
Dominic
 
Share this answer
 
Comments
MK-Gii 13-Mar-17 7:24am    
what if the method runs for ~10-20 seconds?
Will i be sure that GC does not clean stuff and this way distorts the actual values?
Dominic Abraham 13-Mar-17 7:38am    
I don't think this will distorts any values. GC.GetTotalMemory(true) , it will wait for short interval as system collects garbage.
MK-Gii 13-Mar-17 7:47am    
Ok. Lets imagine the situation.
1. Getting the memory usage before the method call (lets say it's 10 MBs)
2. Starting method and it does the following:
2.1. Stored 15 MB of text in memory.
2.2. processes that string data and stores result (50KB) in memory.
2.3. Text data is no longer required so GC collects it.
2.4. method completes.
3. Getting memory usage again with GC.GetTotalMemory(true).

Now here I would expect it to return 10MB + 50KB (10.05 MB) and that is bad since I miss the main stat - how much memory is required for my method to process. So I should have received 15.05MB (the total memory used by the specific method)

Or am I wrong somewhere?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900