I need to set max limit of CPU/RAM usage to some thread. How can this be done by means of .net(without using virtualization)?
-
By "RAM", do you mean stack or heap? You have *some* flexibility on the stack size, but the heap and CPU? not so much AFAIK. – Marc Gravell Jul 23 '12 at 07:10
3 Answers
AFAIK you cannot do it per-thread. You can only do it per-process. The only thing you can do is described here

- 2,594
- 2
- 22
- 36
-
-
@leppie You can set the processor affinity for a process, which can effectively limit its CPU usage. Not sure about memory...don't think that's possible. – Cody Gray - on strike Jul 23 '12 at 07:35
-
1For limiting RAM you can use http://msdn.microsoft.com/en-us/library/windows/desktop/ms684161%28v=vs.85%29.aspx – Eiver Jul 23 '12 at 07:38
Windows does not provide a mechanism to limit the CPU or memory usage of a thread.
However, you can achieve a similar effect by adjusting the priority of your thread. Threads with a higher priority are given a relatively greater share of the computer's resources, while those with a lower priority are down-scheduled when necessary to make room for threads with a higher priority.
In the world of .NET, this is conveniently exposed through the Thread.Priority property, which accepts one of the ThreadPriority values.
Do note that both threads and processes have priority levels, and the base priority level of your thread will be determined both from the value you set for the Thread.Priority property and its process's priority.
Related question: How can I programmatically limit my program's CPU usage to below 70%?

- 1
- 1

- 239,200
- 50
- 490
- 574
to ensure you have enough amount of memory, and because a "single Thread" is handling a limited amount of ram already,
really use apartmentstate with MTA.
It's few Threads but managed by main one, It's achieving process and Tasks without a 'wall of memory limit'. Your Process won't crash with apartment by MTA.
https://learn.microsoft.com/en-us/dotnet/api/system.threading.apartmentstate?view=net-6.0 (one .Net version for MTA apartmentstate )
To earn at speed, or flexible container sure there are :
- parallel Tasking ( Thread.subtree.subtree....parallel... )
- Threadpool
- MTA ( against STA )
- Priority level of thread
- light code
- Memory management by code ( vb : nothing , arrays erase , gc callings )
Every one of those can push your software to be faster, or without memory overload, by speed, synchronisation, global amout of memory, or memory management.

- 17
- 4