简体   繁体   中英

Memory usage in C#

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?

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

虽然我同意你已经收到的关于你的问题的评论,但如果你真的决定采取这种行动,那么使用System.Environment.WorkingSet可能是一个真正的答案吗?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM