public static void ClearFileSystemCache(bool ClearStandbyCache)
        {
            SetIncreasePrivilege(SE_INCREASE_QUOTA_NAME);

            //clear the active file system cache
            int iReturn;
            if (!Is64BitMode())
            {
                SYSTEM_CACHE_INFORMATION info = new SYSTEM_CACHE_INFORMATION();
                info.MinimumWorkingSet = uint.MaxValue; //means to clear the active file system cache
                info.MaximumWorkingSet = uint.MaxValue; //means to clear the active file system cache
                int iSize = Marshal.SizeOf(info);

                GCHandle gch = GCHandle.Alloc(info, GCHandleType.Pinned);
                iReturn = NtSetSystemInformation(SYSTEMCACHEINFORMATION, gch.AddrOfPinnedObject(), iSize);
                gch.Free();
            }
            else
            {
                SYSTEM_CACHE_INFORMATION_64_BIT info = new SYSTEM_CACHE_INFORMATION_64_BIT();
                info.MinimumWorkingSet = -1; //means to clear the active file system cache
                info.MaximumWorkingSet = -1; //means to clear the active file system cache
                int iSize = Marshal.SizeOf(info);

                GCHandle gch = GCHandle.Alloc(info, GCHandleType.Pinned);
                iReturn = NtSetSystemInformation(SYSTEMCACHEINFORMATION, gch.AddrOfPinnedObject(), iSize);
                gch.Free();
            }

            if (iReturn != 0)
            {
                throw new Exception("NtSetSystemInformation(SYSTEMCACHEINFORMATION) error: ", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()));
            }

            if (ClearStandbyCache)
            {
                try
                {
                    SetIncreasePrivilege(SE_PROFILE_SINGLE_PROCESS_NAME);


                    int iSize = Marshal.SizeOf(ClearStandbyPageList);
                    GCHandle gch = GCHandle.Alloc(ClearStandbyPageList, GCHandleType.Pinned);
                    iReturn = NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION, gch.AddrOfPinnedObject(), iSize);
                    gch.Free();

                    if (iReturn != 0)
                    {
                        throw new Exception("NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION) error: ", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()));
                    }
                }
                catch (Exception ex)
                {
                    Context.TraceEvent(1, 101, "Problem clearing standby cache with API call: " + ex.Message);

                    //this is a fallback if the API call doesn't work on an older OS
                    ClearStandbyFileSystemCacheByConsumingAvailableMemory();
                }
            }
        }
        public static void ClearFileSystemCache(bool clearStandbyCache)
        {
            SetIncreasePrivilege(SE_INCREASE_QUOTA_NAME);

            // clear the active file system cache
            int iReturn;

            if (!Is64BitMode())
            {
                SYSTEM_CACHE_INFORMATION info = new SYSTEM_CACHE_INFORMATION();
                info.MinimumWorkingSet = uint.MaxValue; // means to clear the active file system cache
                info.MaximumWorkingSet = uint.MaxValue; // means to clear the active file system cache
                int iSize = Marshal.SizeOf(info);

                GCHandle gch = GCHandle.Alloc(info, GCHandleType.Pinned);
                iReturn = NtSetSystemInformation(SYSTEMCACHEINFORMATION, gch.AddrOfPinnedObject(), iSize);
                gch.Free();
            }
            else
            {
                SYSTEM_CACHE_INFORMATION_64_BIT info = new SYSTEM_CACHE_INFORMATION_64_BIT();
                info.MinimumWorkingSet = -1; // means to clear the active file system cache
                info.MaximumWorkingSet = -1; // means to clear the active file system cache
                int iSize = Marshal.SizeOf(info);

                GCHandle gch = GCHandle.Alloc(info, GCHandleType.Pinned);
                iReturn = NtSetSystemInformation(SYSTEMCACHEINFORMATION, gch.AddrOfPinnedObject(), iSize);
                gch.Free();
            }

            if (iReturn != 0)
            {
                throw new Exception("NtSetSystemInformation(SYSTEMCACHEINFORMATION) error: ", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()));
            }

            if (clearStandbyCache)
            {
                try
                {
                    SetIncreasePrivilege(SE_PROFILE_SINGLE_PROCESS_NAME);

                    int      iSize = Marshal.SizeOf(ClearStandbyPageList);
                    GCHandle gch   = GCHandle.Alloc(ClearStandbyPageList, GCHandleType.Pinned);
                    iReturn = NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION, gch.AddrOfPinnedObject(), iSize);
                    gch.Free();

                    if (iReturn != 0)
                    {
                        throw new Exception("NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION) error: ", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Problem clearing standby cache with API call: " + ex.Message);

                    // this is a fallback if the API call doesn't work on an older OS
                    ClearStandbyFileSystemCacheByConsumingAvailableMemory();
                }
            }
        }
Exemple #3
0
        //Function used to clear file system cache, returns boolean
        public void ClearFileSystemCache(bool ClearStandbyCache)
        {
            try
            {
                //Check if privilege can be increased
                if (SetIncreasePrivilege(SE_INCREASE_QUOTA_NAME))
                {
                    uint     num1;
                    int      SystemInfoLength;
                    GCHandle gcHandle;
                    //First check which version is running, then fill structure with cache information. Throw error is cache information cannot be read.
                    if (!Is64BitMode())
                    {
                        SYSTEM_CACHE_INFORMATION cacheInformation = new SYSTEM_CACHE_INFORMATION();
                        cacheInformation.MinimumWorkingSet = uint.MaxValue;
                        cacheInformation.MaximumWorkingSet = uint.MaxValue;
                        SystemInfoLength = Marshal.SizeOf(cacheInformation);
                        gcHandle         = GCHandle.Alloc(cacheInformation, GCHandleType.Pinned);
                        num1             = NtSetSystemInformation(SystemFileCacheInformation, gcHandle.AddrOfPinnedObject(), SystemInfoLength);
                        gcHandle.Free();
                    }
                    else
                    {
                        SYSTEM_CACHE_INFORMATION_64_BIT information64Bit = new SYSTEM_CACHE_INFORMATION_64_BIT();
                        information64Bit.MinimumWorkingSet = -1L;
                        information64Bit.MaximumWorkingSet = -1L;
                        SystemInfoLength = Marshal.SizeOf(information64Bit);
                        gcHandle         = GCHandle.Alloc(information64Bit, GCHandleType.Pinned);
                        num1             = NtSetSystemInformation(SystemFileCacheInformation, gcHandle.AddrOfPinnedObject(), SystemInfoLength);
                        gcHandle.Free();
                    }
                    if (num1 != 0)
                    {
                        throw new Exception("NtSetSystemInformation(SYSTEMCACHEINFORMATION) error: ", new Win32Exception(Marshal.GetLastWin32Error()));
                    }
                }

                //If passes paramater is 'true' and the privilege can be increased, then clear standby lists through MemoryPurgeStandbyList
                if (ClearStandbyCache && SetIncreasePrivilege(SE_PROFILE_SINGLE_PROCESS_NAME))
                {
                    int      SystemInfoLength = Marshal.SizeOf(MemoryPurgeStandbyList);
                    GCHandle gcHandle         = GCHandle.Alloc(MemoryPurgeStandbyList, GCHandleType.Pinned);
                    uint     num2             = NtSetSystemInformation(SystemMemoryListInformation, gcHandle.AddrOfPinnedObject(), SystemInfoLength);
                    gcHandle.Free();
                    if (num2 != 0)
                    {
                        throw new Exception("NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION) error: ", new Win32Exception(Marshal.GetLastWin32Error()));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }
        /// <summary>
        /// Clear the active file system cache
        /// </summary>
        public static void ClearFileSystemCache(bool clearStandbyCache)
        {
            SetIncreasePrivilege(SE_INCREASE_QUOTA_NAME);

            int result;

            if (!Is64BitMode())
            {
                var info = new SYSTEM_CACHE_INFORMATION
                {
                    MinimumWorkingSet = uint.MaxValue, //means to clear the active file system cache
                    MaximumWorkingSet = uint.MaxValue  //means to clear the active file system cache
                };
                int iSize = Marshal.SizeOf(info);

                var gch = GCHandle.Alloc(info, GCHandleType.Pinned);
                try
                {
                    result = NtSetSystemInformation(SYSTEMCACHEINFORMATION, gch.AddrOfPinnedObject(), iSize);
                }
                finally
                {
                    gch.Free();
                }
            }
            else
            {
                var info = new SYSTEM_CACHE_INFORMATION_64_BIT
                {
                    MinimumWorkingSet = -1, //means to clear the active file system cache
                    MaximumWorkingSet = -1  //means to clear the active file system cache
                };
                int iSize = Marshal.SizeOf(info);

                var gch = GCHandle.Alloc(info, GCHandleType.Pinned);
                try
                {
                    result = NtSetSystemInformation(SYSTEMCACHEINFORMATION, gch.AddrOfPinnedObject(), iSize);
                }
                finally
                {
                    gch.Free();
                }
            }

            if (result != 0)
            {
                throw new ApplicationException("NtSetSystemInformation(SYSTEMCACHEINFORMATION) error", new Win32Exception(Marshal.GetLastWin32Error()));
            }

            if (clearStandbyCache)
            {
                try
                {
                    SetIncreasePrivilege(SE_PROFILE_SINGLE_PROCESS_NAME);

                    int iSize = Marshal.SizeOf(ClearStandbyPageList);
                    var gch   = GCHandle.Alloc(ClearStandbyPageList, GCHandleType.Pinned);
                    try
                    {
                        result = NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION, gch.AddrOfPinnedObject(), iSize);
                    }
                    finally
                    {
                        gch.Free();
                    }

                    if (result != 0)
                    {
                        throw new ApplicationException("NtSetSystemInformation(SYSTEMMEMORYLISTINFORMATION) error", new Win32Exception(Marshal.GetLastWin32Error()));
                    }
                }
                catch
                {
                    //this is a fallback if the API call doesn't work on an older OS
                    ClearStandbyFileSystemCacheByConsumingAvailableMemory();
                }
            }
        }
Exemple #5
0
 public static extern uint ZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,
     ref SYSTEM_CACHE_INFORMATION SystemInformation, int SystemInformationLength, out int ReturnLength);
Exemple #6
0
 public static extern uint ZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,
                                                    ref SYSTEM_CACHE_INFORMATION SystemInformation, int SystemInformationLength, out int ReturnLength);