18

I have a program that uses threads in C#. Is there a way to know programmatically the memory usage of the application? I want to limit the spawning of threads to say 10 megabytes of memory, how would I do that?

yoitsfrancis
  • 4,278
  • 14
  • 44
  • 73
  • 19
    The minute you start down the road to control memory is the minute you start making a mistake. If you have REQUIREMENTS for memory, don't use .NET. If you don't have requirements, stop worrying about memory. .NET manages memory better than you ever could. Stop it. Leave it alone. No. Bad. Stop. –  Apr 16 '09 at 12:36
  • 1
    I agree with Will. As long as your currently referenced objects are below 2 GBs of memory on 32 bit system, let the GC handle it. – Steve Apr 16 '09 at 12:38
  • 1
    @Will: Why is this bad? If not .NET then what else would be used? – AnthonyWJones Apr 16 '09 at 12:48
  • 1
    Its bad because the GC is specifically tuned and tested to manage memory by people with letters like PhD after their names. Are you an expert in memory management? The only guarantee you have is that what you do will be less efficient than the GC, which means you defeat your own purpose. Stop it. –  Apr 16 '09 at 12:51
  • 1
    If you have specific memory requirements that aren't BS or can't be managed away (HURR DURR LOOK AT ALL THE MEMORY UR PROGRAM TAKES IN TASK MANAGER HURR DURR) then you need to code in C++ or C, something close to the wire where YOU control memory allocation. Personally, I'm for saying F that noise. –  Apr 16 '09 at 12:53
  • @yoitsfrancis: I think you need to beef up your question with the reasons why you want this limit. If you are saying 10Mb per thread despite the number of threads being created then I agree with Will this is bad. – AnthonyWJones Apr 16 '09 at 13:08
  • 3
    @yoitsfrancis: OTH you have a service app that you want to prevent being a memory hog then you need to state that, I don't think that such a requirement is unreasonable and its not the job of the GC to limit demand for memory. – AnthonyWJones Apr 16 '09 at 13:12
  • 5
    I don't think he's trying to manage his memory in regards to making each thread 10Mb or less. I think he's trying to manage the number of spawned threads such that his process does not exceed 10Mb, which I think is a fair request. It's up to him to decide if his application warrants this. – Joseph Apr 16 '09 at 13:16

4 Answers4

25

If you want the memory of the entire running process and not on a per thread basis, how about:

// get the current process
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

// get the physical mem usage
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;

There's a whole host of other process memory properties besides WorkingSet64 check out the "memory related" ones at the following link for the one that best suit

http://msdn.microsoft.com/en-us/library/system.diagnostics.process_properties.aspx

binarybob
  • 3,529
  • 3
  • 23
  • 21
4

While I agree with the comments you've already received on your question, the use of System.Environment.WorkingSet might perhaps be an actual answer to it if you really decide to take this course of action?

peSHIr
  • 6,279
  • 1
  • 34
  • 46
1

I'm with Will and Steve, don't do this unless you REALLY have to do it . . . that said . . .

if you really need to do it you can use the .Net Hosting APIs they're there so apps like SQL Server can host the .net framework within the application.

It gives you control over memory management etc, I've read the article but never used the API's, they're on my to-do list for some night when I'm bored and none of my other "fun" projects feel like fun that week :)

Hope this helps.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
1

You don't measure the number of threads in memory usage. Rather, take into account the number of "processors" (SMP / Multi-Cores / Hyper-Threading) to decide how many threads should run in parallel. Or use the ThreadPool, which is sized automatically to achieve a good thread-to-CPU ratio.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Lucero
  • 59,176
  • 9
  • 122
  • 152
  • Mhmm... you are generalising. What if thread can use unpredictable amount of memory ? – majkinetor Apr 16 '09 at 13:10
  • If the amount is unpredictable, you'll only know that you've run into trouble when the thread is already running. Therefore, if you are processing data which may lead to heavy memory usage, you'll always have to take care of this issue - no matter how many threads are - or will be - running. – Lucero Apr 16 '09 at 15:06