none
Suspend .NET garbage collection RRS feed

  • Question

  • As the title says I am trying to pause the .net garbage collection for a short period of time.

    Firstly, if you are one of those people who is going to tell me that you should never pause the GC, DONT POST!!!! I don't want you telling me why I shouldn't do it, I need to know how to.

    The .NET GC is occurring in the middle of a large time consuming function which is delaying a scudo real-time process. This is a quintessential function for the process that already has an elevated priority and is being interrupted by the GC causing a 'lag' in an algorithm that then dominos to the rest of the program.

    The code is a steering and IO management function that reads the state of numerous sensor from memory and then corrects the steering angle and analyses the IO data for unexpected issues. because this function calls and accepts return values from over 100 other functions GC kicks in and stalls the operation of the function 3 or 4 times in just 1 iteration. This function is running at 100Hz, and needs to be pushed to about 120Hz.

    The simplest way to do this is disable GC at the beginning of the function, and then re-enable it at the end. GC will then take care of any unreferenced data, and will only fire once per function iteration.

    If you are providing source code to do this, please use C# or VB. I will translate it there.

    And again, if you are going to tell me that I am doing this the wrong way and the you should never disable GC, please just don't reply.

    ~Ben

    Saturday, September 9, 2017 2:06 AM

Answers

  • Have you considered GC.TryStartNoGCRegion and switching to .NET 4.6?

    • Proposed as answer by MDeero Saturday, September 9, 2017 1:12 PM
    • Marked as answer by NavyIsland Wednesday, October 25, 2017 8:41 PM
    Saturday, September 9, 2017 6:29 AM

All replies

  • Have you considered GC.TryStartNoGCRegion and switching to .NET 4.6?

    • Proposed as answer by MDeero Saturday, September 9, 2017 1:12 PM
    • Marked as answer by NavyIsland Wednesday, October 25, 2017 8:41 PM
    Saturday, September 9, 2017 6:29 AM
  • Hi Navylsland,

    Thank you for posting here.

    Or you could try to use GC.EndNoGCRegion Method (). It is available since 4.6.

    Best Regards,

    Wendy



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 12, 2017 2:20 AM
    Moderator