david Posted January 15, 2008 Share Posted January 15, 2008 How can I monitor a certain programs memory usage. I basically want the program to kill that process if the memory usage exeeds 10k. Help? Link to comment Share on other sites More sharing options...
0 Wilhelmus Posted January 16, 2008 Share Posted January 16, 2008 You could try something like this. Imports System Imports System.Diagnostics Imports System.ComponentModel Public Class Form1 ' ProcessMonitor() Sub ProcessMonitor() ' GetProcessesByName(), our victim is Notepad... Dim processes() As Process = Process.GetProcessesByName("notepad") ' Found something? If processes.Length > 0 Then Dim cnt As Integer ' Loop For cnt = 0 To processes.Length - 1 ' Process is still running? If Not processes(cnt).HasExited Then ' Exceeds limit? ' WorkingSet64: The amount of physical memory, in bytes, allocated for the associated process If (processes(cnt).WorkingSet64 / 1024) > 10000 Then ' Set Form caption Text = "WARNING! Notepad exceeds limit, killing it!" ' Try killing it... Try processes(cnt).Kill() Catch ex As Exception MessageBox.Show(ex.Message) End Try End If ' If WorkingSet64 End If ' If not Next 'For cnt End If ' If processes.Length > 0 Then End Sub ' Then call it... ' For example, in Button1_Click(): Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ProcessMonitor() End Sub End Class Link to comment Share on other sites More sharing options...
0 david Posted January 16, 2008 Author Share Posted January 16, 2008 Wow. Thanks so much! Link to comment Share on other sites More sharing options...
Question
david
How can I monitor a certain programs memory usage. I basically want the program to kill that process if the memory usage exeeds 10k. Help?
Link to comment
Share on other sites
2 answers to this question
Recommended Posts