static private long getSize(IntPtr pHandle)
        {
            PROCESS_MEMORY_COUNTERS pCounters = new PROCESS_MEMORY_COUNTERS();

            GetProcessMemoryInfo(pHandle, out pCounters, (uint)Marshal.SizeOf(pCounters));
            return(pCounters.WorkingSetSize.ToInt64());
        }
示例#2
0
        public static int GetMemoryUsageForProcess(uint pid)
        {
            int mem     = 0;
            int pHandle = OpenProcess(0x0400 | 0x0010, 0, pid);
            PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();

            if (GetProcessMemoryInfo(pHandle, pmc, 40) != 0)
            {
                mem = pmc.WorkingSetSize;
            }
            CloseHandle(pHandle);
            return(mem);
        }
示例#3
0
        public static unsafe long GetCurrentWorkingSet()
        {
            PROCESS_MEMORY_COUNTERS pr = new PROCESS_MEMORY_COUNTERS();

            pr.cb = (uint)sizeof(PROCESS_MEMORY_COUNTERS);

            if (GetProcessMemoryInfo(currentProcessHandle, out pr, pr.cb) == false)
            {
                throw new Win32Exception();
            }

            return((long)pr.WorkingSetSize.ToUInt64());
        }
示例#4
0
        public InfoDisplay(Viewer viewer)
        {
            Viewer = viewer;
            Logger = new DataLogger(Path.Combine(Viewer.Settings.LoggingPath, "OpenRailsDump.csv"));

            ProcessHandle         = OpenProcess(0x410 /* PROCESS_QUERY_INFORMATION | PROCESS_VM_READ */, false, Process.GetCurrentProcess().Id);
            ProcessMemoryCounters = new PROCESS_MEMORY_COUNTERS()
            {
                cb = 40
            };

            if (Viewer.Settings.DataLogger)
            {
                DataLoggerStart(Viewer.Settings);
            }
        }
示例#5
0
        public static PROCESS_MEMORY_COUNTERS GetProcessMemoryUsage(int PID)
        {
            int hProcess = OpenProcess(ProcessRights.QUERY_INFORMATION, false, (uint)PID);
            PROCESS_MEMORY_COUNTERS pmc1 = new PROCESS_MEMORY_COUNTERS();

            try
            {
                if (GetProcessMemoryInfo(hProcess, ref pmc1, 40) == 0)
                {
                    throw new Exception("GetProcessMemoryInfo failed for PID=" + PID.ToString());
                }
            }
            finally
            {
                CloseHandle(hProcess);
            }
            return(pmc1);
        }
示例#6
0
            public static long GetMemoryUsageForProcess(long pid)
            {
                long mem     = 0;
                int  pHandle = OpenProcess(0x0400 | 0x0010, 0, (uint)pid);

                try
                {
                    PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
                    if (GetProcessMemoryInfo(pHandle, pmc, 40) != 0)
                    {
                        mem = pmc.WorkingSetSize;
                    }
                }
                finally
                {
                    CloseHandle(pHandle);
                }
                return(mem);
            }
示例#7
0
文件: Form1.cs 项目: bshultz/ctasks
        private void OnTimer_Tick(object sender, EventArgs e)
        {
            // update status bar
            if (m_processHandle != IntPtr.Zero)
            {
                // memory
                var counters = new PROCESS_MEMORY_COUNTERS();
                GetProcessMemoryInfo(m_processHandle, out counters, 40);
                m_sslMemory.Text = string.Format("Memory usage: {0:#,##0} KBytes", counters.WorkingSetSize / 1024);
                m_sslMemory.Invalidate();

                // gdi
                m_sslGdi.Text = string.Format("GDI Objects: {0}", GetGuiResources(m_processHandle, (uint)ResourceType.Gdi));
                m_sslGdi.Invalidate();

                // objects
                m_sslObjectCount.Text = string.Format("Calendars Created: {0}", m_controlcount);
                m_sslObjectCount.Invalidate();
            }
        }
示例#8
0
            public static StringDictionary GetEnvironmentVariables(UInt32 pid)
            {
                ProcessAccessFlags flags = ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VMRead;

                IntPtr hProcess = IntPtr.Zero;

                hProcess = OpenProcess(flags, false, (int)pid);
                if (hProcess == IntPtr.Zero)
                {
                    throw new System.ComponentModel.Win32Exception(GetLastError());
                }
                IntPtr penv = GetPenv(hProcess);

                try {
                    uint size;
                    PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
                    if (!GetProcessMemoryInfo(hProcess, out pmc, out size))
                    {
                        throw new System.ComponentModel.Win32Exception(GetLastError());
                    }
                    int       dataSize   = (int)pmc.WorkingSetSize;
                    const int maxEnvSize = 32767;
                    if (dataSize > maxEnvSize)
                    {
                        dataSize = maxEnvSize;
                    }
                    byte[] envData = new byte[dataSize];
                    IntPtr res_len = IntPtr.Zero;
                    bool   b       = ReadProcessMemory(hProcess, penv, envData, new IntPtr(dataSize), ref res_len);
                    if (!b || (int)res_len != dataSize)
                    {
                        throw new System.ComponentModel.Win32Exception(GetLastError());
                    }
                    return(EnvToDictionary(envData));
                } finally {
                    CloseHandle(hProcess);
                }
            }
示例#9
0
 private static extern int GetProcessMemoryInfo(int hProcess, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, uint cb);
示例#10
0
 private static extern int GetProcessMemoryInfo(int hProcess, [Out] PROCESS_MEMORY_COUNTERS counters, int size);
 internal static extern bool GetProcessMemoryInfo(IntPtr Process, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, uint cb);
示例#12
0
 public static StringDictionary GetEnvironmentVariables(UInt32 pid)
 {
     ProcessAccessFlags flags = ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VMRead;
     IntPtr hProcess = IntPtr.Zero;
     hProcess = OpenProcess(flags, false, (int)pid);
     if (hProcess == IntPtr.Zero) {
     throw new System.ComponentModel.Win32Exception(GetLastError());
     }
     IntPtr penv = GetPenv(hProcess);
     try {
     uint size;
     PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
     if (!GetProcessMemoryInfo(hProcess, out pmc, out size)) {
     throw new System.ComponentModel.Win32Exception(GetLastError());
     }
     int dataSize = (int)pmc.WorkingSetSize;
     const int maxEnvSize = 32767;
     if (dataSize > maxEnvSize) {
     dataSize = maxEnvSize;
     }
     byte[] envData = new byte[dataSize];
     IntPtr res_len = IntPtr.Zero;
     bool b = ReadProcessMemory(hProcess, penv, envData, new IntPtr(dataSize), ref res_len);
     if (!b || (int)res_len != dataSize) {
     throw new System.ComponentModel.Win32Exception(GetLastError());
     }
     return EnvToDictionary(envData);
     } finally {
     CloseHandle(hProcess);
     }
 }
示例#13
0
 private static extern bool GetProcessMemoryInfo(IntPtr hProcess, [In][Out] PROCESS_MEMORY_COUNTERS counters, uint size);
示例#14
0
 public static extern bool GetProcessMemoryInfo(IntPtr hProcess, [In, Out] ref PROCESS_MEMORY_COUNTERS counters, uint size);
示例#15
0
 public static extern int GetProcessMemoryInfo(IntPtr hProcess, out
                                               PROCESS_MEMORY_COUNTERS counters, int size);
示例#16
0
        private void TimerCallback(object state)
        {
            try
            {
                // Get process handles.
                var process = Process.GetCurrentProcess();
                var pid     = process.Id;
                var handle1 = process.Handle;

                if (!_initialEmptyComplete)
                {
                    // The first time through, empty the working set and return.
                    _initialEmptyComplete = true;
                    NativeMethods.EmptyWorkingSet(handle1);
                    return;
                }

                // Get memory usage.
                var safeHandle = SafeProcessHandle.OpenProcess(pid, ProcessAccessFlags.QueryInformation);
                var handle2    = safeHandle.DangerousGetHandle();
                var counters   = new PROCESS_MEMORY_COUNTERS();
                NativeMethods.GetProcessMemoryInfo(handle2, out counters, (uint)Marshal.SizeOf(counters));

                var workingSetSize = counters.WorkingSetSize / 1024 / 1024;
                if (workingSetSize < _mediumThreshold)
                {
                    // We're below the medium threshold, nothing to do.
                    return;
                }

                if (workingSetSize < _highThreshold)
                {
                    // We're above the medium threshold and below the high threshold. Get the
                    // time since the last empty and bail if it was less than 10 minutes ago.
                    var timeSinceLastEmpty = TimeSpan.FromTicks(
                        Stopwatch.GetTimestamp() - _lastEmptyTimestamp);

                    if (timeSinceLastEmpty < TimeSpan.FromMinutes(10))
                    {
                        return;
                    }
                }

                // Empty the working set.
                NativeMethods.EmptyWorkingSet(handle1);
                _lastEmptyTimestamp = Stopwatch.GetTimestamp();

                // Get updated memory usage.
                NativeMethods.GetProcessMemoryInfo(handle2, out counters, (uint)Marshal.SizeOf(counters));

                // Record the operation in the log.
                var newWorkingSetSize = counters.WorkingSetSize / 1024 / 1024;
                var highUsageDetected = workingSetSize > _highThreshold;

                Logger.Info(
                    "{0} memory threshold ({1}MB) exceeded - working set reduced from: {2} to: {3}",
                    highUsageDetected ? "High" : "Medium",
                    highUsageDetected ? _highThreshold : _mediumThreshold,
                    workingSetSize,
                    newWorkingSetSize);
            }
            catch (Exception e)
            {
                Logger.Error("Error monitoring working set size", e);
            }
            finally
            {
                Thread.MemoryBarrier();
                if (!_disposed)
                {
                    _timer.Change(TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(-1));
                }
            }
        }
示例#17
0
 static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);
示例#18
0
 private static unsafe extern bool GetProcessMemoryInfo(System.IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);
示例#19
0
文件: GDITest.cs 项目: bshultz/ctasks
 private static extern int GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, int size);
示例#20
0
        // Запуск Windows приложения
        public static bool regular(
            string executable,
            ref uint memory,
            ref uint time
            )
        {
            // Информация о процессе
            pi = new PROCESS_INFORMATION();

            // Момент запуска дочернего процесса
            SYSTEMTIME startTime = new SYSTEMTIME();

            GetSystemTime(ref startTime);

            // Запускаем дочерний процесс
            bool result = CreateProcess(
                executable,
                null,
                IntPtr.Zero,
                IntPtr.Zero,
                inheritance,
                0,
                IntPtr.Zero,
                null,
                ref si,
                ref pi
                );

            if (!result)
            {
                return(false);
            }

            // Ожидание завершения процесса
            uint res_code = WaitForSingleObject(pi.hProcess, (uint)time);

            if (res_code == WAIT_TIMEOUT)
            {
                TerminateProcess(pi.hProcess, (uint)0);
            }
            else if (res_code != WAIT_OK)
            {
                return(false);
            }

            // Момент завершения дочернего процесса
            SYSTEMTIME stopTime = new SYSTEMTIME();

            GetSystemTime(ref stopTime);

            // Время работы в миллисекундах
            time =
                Abs(stopTime.wHour - startTime.wHour) * 60 * 60 * 1000 +
                Abs(stopTime.wMinute - startTime.wMinute) * 60 * 1000 +
                Abs(stopTime.wSecond - startTime.wSecond) * 1000 +
                Abs(stopTime.wMilliseconds - startTime.wMilliseconds);

            // Получим данные об использовании памяти
            PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();

            pmc.cb = (uint)Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS));
            result = GetProcessMemoryInfo(pi.hProcess, ref pmc, pmc.cb);

            // Память использованная процессом
            memory = pmc.PeakPagefileUsage;

            // Завершаем работу с дескрипторами
            CloseHandle(pi.hProcess);

            return(true);
        }
示例#21
0
 static extern bool GetProcessMemoryInfo(
     IntPtr hProcess,                                // HANDLE
     ref PROCESS_MEMORY_COUNTERS counters,           // PPROCESS_MEMORY_COUNTERS
     uint size                                       // DWORD
     );
示例#22
0
 static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);
示例#23
0
 private static long getSize(IntPtr pHandle)
 {
     PROCESS_MEMORY_COUNTERS pCounters = new PROCESS_MEMORY_COUNTERS();
     GetProcessMemoryInfo(pHandle, out pCounters, (uint)Marshal.SizeOf(pCounters));
     return pCounters.WorkingSetSize.ToInt64();
 }
示例#24
0
        public static unsafe long GetCurrentWorkingSet()
        {
            PROCESS_MEMORY_COUNTERS pr = new PROCESS_MEMORY_COUNTERS();
            pr.cb = (uint)sizeof (PROCESS_MEMORY_COUNTERS);

            if (GetProcessMemoryInfo(currentProcessHandle, out pr, pr.cb) == false)
                throw new Win32Exception();

            return (long)pr.WorkingSetSize.ToUInt64();
        }
示例#25
0
 private unsafe static extern bool GetProcessMemoryInfo(System.IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);