• 0

[vb.net] monitor process' memory usage


Question

2 answers to this question

Recommended Posts

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.