M
M
Maxim Isaev2019-10-04 19:22:34
IT education
Maxim Isaev, 2019-10-04 19:22:34

How to control memory in C#?

Good day, how to set a limit for the program, for example, 100mb?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lelushak, 2019-10-05
@MaximIs

In general, no way. What for? The only possible recommendation here is to write code in such a way that a lot of memory is not wasted.
Hypothetically, it is possible to develop a special architecture for the application, which will allow any running tasks to be interrupted when the memory threshold is exceeded, release resources and then wait for the GC to be called. For example, via TPL with CancellationToken. But it is, to put it mildly, a difficult path. The occupied memory can be monitored via GC.GetTotalMemory or OS tools. But there are two more nuances:
1. Constant measurement of memory will lead to a drop in the overall performance of the program
2. You will not be able to use libraries that do not support canceling called functions

C
crazywu, 2019-10-04
@crazywu

If you are talking about direct memory reading/writing, then read about unsafe and pointers in c#.
If you are talking about gc, then in order to understand what is happening well, you should read CLR VIA C #, Jeffrey Richter
PS Otherwise, fine control of container sizes, etc. can help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question