Skip to content

API proposal: add function to flush caches #30885

Open

Description

@gbalykov

Rationale and Usage

The idea of this API is to allow flushing of PE caches at request, which will allow to reduce memory consumption of the process.

Type type = typeof(RuntimeHelpers);
MethodInfo method = type.GetMethod("FlushPECaches");
if (method != null)
  method.Invoke(null, new object[]{});

After this call Private_Clean and Shared_Clean of the process are reduced, as well as PSS and RSS.

Proposed API

namespace System.Runtime.CompilerServices
{
    public static partial class RuntimeHelpers
    {
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        public static extern void FlushPECaches();
    }
}

Details

  • Internally, FlushPECaches tries to mark all readonly pages of PE images (this considers relocations too) as not needed so that they can be freed. This is done using madvise with MADV_DONTNEED.
  • both dlls and ni.dlls are considered

Pull Request

PR with proposed change is available at dotnet/coreclr#26507

cc @alpencolt

Activity

  1. jkotas commented on Sep 18, 2019

    @jkotas
    Member

    I think this API should be about flushing all caches that the runtime controls: trimming unnecessary pages in GC heap, etc.

  2. transferred this issue fromdotnet/corefxon Feb 1, 2020
  3. added
    untriaged
    on Feb 23, 2020
  4. added this to the Future milestone on Jun 24, 2020
  5. removed
    untriaged
    on Jul 9, 2020
  6. ashaurtaev commented on May 14, 2024

    @ashaurtaev
    Contributor

    Rationale and Usage

    API that allows you to clear runtime caches on demand, which will reduce the overall memory consumption of the process.

    We can later expand API and add other types of caches for flushing.

    Usage

    using System.Runtime.CompilerServices;
    ...
    FlushCaches(FlushCachesMode.PE); //Flushing PE clean pages

    After this call Private_Clean and Shared_Clean of the process are reduced, as well as PSS and RSS.

    or

    Type type = typeof(RuntimeHelpers);
    MethodInfo method = type.GetMethod("FlushCaches");
    if (method != null)
      method.Invoke(null, new object[]{FlushCachesMode.GC | FlushCachesMode.Jit});

    After this call Private_Dirty of the process is reduced, as well as PSS and RSS.

    Proposed API

    [Flags]
    private enum FlushCachesMode
    {
        All = ~0,
        PE = 1,
        Jit = 2,
        GC = 4,
    }
    
    public static void FlushCaches(object? mode = null)
    {
        mode ??= FlushCachesMode.All;
        var cacheHandlers = new System.Collections.Generic.Dictionary<FlushCachesMode, Action>
        {
            {FlushCachesMode.GC, FlushGCCaches},
            {FlushCachesMode.Jit, FlushJitCaches},
            {FlushCachesMode.PE, FlushPECaches},
        };
        foreach (var item in cacheHandlers)
        {
            if (((FlushCachesMode)mode & item.Key) == item.Key)
            {
                item.Value.Invoke();
            }
        }
    }
    
    private static void FlushGCCaches()
    {
        GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
        GC.Collect(2, GCCollectionMode.Forced, true, true);
    }
    
    [MethodImplAttribute(MethodImplOptions.InternalCall)]
    public static extern void FlushJitCaches();
    
    [MethodImplAttribute(MethodImplOptions.InternalCall)]
    public static extern void FlushPECaches();

    Details

    • This API marks all readonly PE Images pages as not needed so that they can be freed, reduces the size of the GC Heap and cleans up arena cache used for internal data during JIT compilation.
    • Internally, FlushPECaches tries to mark all readonly pages of PE images, this is done using madvise with MADV_DONTNEED (both dlls and ni.dlls are considered)
    • Flush jit calls JitHost::Reclaim()
    • Flush GC calls GC for max generation with compaction

    Pull Request

    FlushPECaches part in dotnet/coreclr#26507 PR

    @jkotas what do you think about this API?

    cc @gbalykov @t-mustafin

  7. jkotas commented on May 14, 2024

    @jkotas
    Member

    For mapped files, I am not convinced that it makes sense to special case PE files. PE files are not that different from other (native) code in the process. Also, parts of the PE files and other code are immediately going to come back since they are referenced even in steady state. This should be implementable outside the runtime by walking all process memory and paging it out.

    For Jit scratch space cache, this cache is not that big, and there number of similar caches in the core runtime (e.g. ArrayPool) or outside the code runtime in 3rd party libraries. Should all these caches participate somehow?

  8. ashaurtaev commented on May 20, 2024

    @ashaurtaev
    Contributor

    Thanks for your feedback, let me think about it.

  9. changed the title [-]API proposal: add function to flush PE caches[/-] [+]API proposal: add function to flush caches[/+] on Jun 5, 2024
  10. anoop331 commented on Jun 6, 2024

    @anoop331

    Will this api be introduced to net8, and if so, when would it be in place? Or is there a way to get a prelease version otherwise for us to try it out?

  11. jkotas commented on Jun 6, 2024

    @jkotas
    Member

    Will this api be introduced to net8,

    unlikely

  12. anoop331 commented on Jun 6, 2024

    @anoop331

    You mean not in net8 or not ever? Without understanding all the details, at least the intention seems to be a nice hack to bring the resource usage down, especially when the unloading using dlclose() is not supported..

  13. jkotas commented on Jun 6, 2024

    @jkotas
    Member

    The milestone is set to Future. It means that it may happen in some future dotnet version. It does not mean never.

  14. ThaDaVos commented on Oct 25, 2024

    @ThaDaVos

    Any update on when this can be expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions