Forcing GC.Collect () to reduce the chance of running during time-sensitive code?

During the interview, I was asked to come up with a way to ensure that the code block in C # could work in constant time to meet the hypothetical requirements of the time. The interviewer mentioned that one way is to call the garbage collector to collect before the code block is executed so that it significantly reduces the likelihood of the GC repeating during this code block. It has been applied to accurate time measurements of a medical device, where garbage collection can affect these measurements.

It makes sense to me, but I could not find any information to confirm this. The general consensus I reviewed was to never call GC.Collect (), and exceptions did not include this scenario.

Can running GC.Collect () really reduce the likelihood of it starting in the near future? Is this suitable for use with the .NET Framework? Does GC.Collect () compile for other CLR programs, or is it applicable only to the current process?

+5
source share
3 answers

Jared's answer, of course, is excellent. To add a few other points:

Can running GC.Collect () really reduce the likelihood of it starting in the near future?

Yes.

Is this right for using .net framework?

, , , .

: WaitForPendingFinalizers. , , .

GC.Collect() CLR ?

, , , . -.

+13

, GC.Collect , . , , . , , GC.Collect , , , .

, . , . , , GC.Collect .

, GC.Collect ,

  • GC.Collect

.

+12

what you could do is indicate to disable garbage collection / background using app.config: Parallel garbage collection on MSDN

0
source

All Articles