Results 1 to 26 of 26

Thread: cpu & memory usage (solved)

  1. #1

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196

    cpu & memory usage (solved)

    could some1 show me an example of a way to monitor a process' cpu and memory usages?...btw im gonna put the values in 2 progressbars for users to view in my app....thx in advance

    AcE
    Last edited by theonetrueace; Apr 18th, 2003 at 02:15 AM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    You probably want to look into the Process component:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdiagnosticsprocessclasstopic.htm

    Many examples of code are in the help.

  3. #3

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    yea i have....i been reading threw that, lookin on vbcity, lookin on planet-source-code, and lookin on here all day....but i cant find the exact way to get the values......but knowing me i have overlooked them....thats why i posted

    AcE

  4. #4

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    process.workingset gives you the total memory usage....im still looking for cpu...

    AcE

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Hmm well actually the perfmon COM object is probably much more useful for what you seem to be looking to do, although it's kind of overkill.

    ms-help://MS.VSCC/MS.MSDNVS/vbref/html/vbsamwindowsformsperformancemonitorsample.htm

    complete sample app included.

    edit: woops perfmon actually has a .NET version according to the docs. and duh, the component is in the toolbox under Components.
    Last edited by Slow_Learner; Apr 17th, 2003 at 05:33 PM.

  6. #6

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    yea i looked at that earlyer....but i couldnt find a single command to return a number of process cycles or percentage of processor usage....

    AcE

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    The following I lifted off a forum:

    To get the CPU load and other information about system, you may use the
    System.Diagnostics.PerformanceCounter class.The PerformanceCounter
    component can be used for both reading existing predefined or custom
    counters and publishing (writing) performance data to custom counters.

    To read from a performance counter, create an instance of the
    PerformanceCounter class, set the CategoryName, CounterName, and,
    optionally, the InstanceName or MachineName properties, and then call the
    NextValue method to take a performance counter reading.




    VB Code:
    1. Imports System.Diagnostics
    2.  
    3. Dim myCounter as PerformanceCounter = new PerformanceCounter()
    4. myCounter.CategoryName = "Processor"
    5. myCounter.CounterName = "% Processor Time"
    6. myCounter.InstanceName = "_Total"
    7.  
    8. msgbox("The Processor Time: " &  myCounter.RawValue)
    9. 'or  Label1.Text = myCounter.RawValue

    The value returned is the reading at that time... what you can do to illustrate it better is declare myCounter as Private at the Form LEvel, then run the rest of the code underneath a button click...

    InstanceName has to do with how many processors or which processor you want to monitor, in this case, its set to Total to measure all processors on the machine.. which is most likely just 1.

    The machine name determines which computer should be monitored.
    The category name is the category the counter is under, such as Processor, Paging File, and PhysicalDisk.
    The counter name indicates the specific counter, such as % Processor Time.
    The instance name determines the instance of the counter. For example, a four-processor server has four instances of the % Processor Time counter, plus a total counter.

    -ZDNET australia
    http://www.zdnet.com.au/builder/prog...0272791,00.htm
    Last edited by nemaroller; Apr 17th, 2003 at 06:13 PM.

  8. #8

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    yup yup....i saw that....it wuz in the sample code.....but i dont really understand the concept here.....its returning the processor "time" ... wut does that mean in this case?

    AcE

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    In this case its returning a value between 0 and 100% ....

    because the category name is % Processor Time

  10. #10

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    so heres wut i got when i put it in a textbox

    The Processor Time: 210510499232

    ..... how u get that number to a number from 1-100???
    im sorry if i seem blind.....i usually have no problem with these type things....

    AcE

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yea I know, for some reason, the first value (the first sampling of the value ) is always some messed up number...

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Maybe its a bug.... just read it once when after its initialized and throw away that value...

  13. #13

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    heres something else thats interesting to me......the workingset property can only be accessed 1 time per start of the process.....

    well it is definity a time based number bc it gets larger everytime i hit my button.....

    edit: nm got that figured out...u got to run the process.refresh call before u call process.workingset....still dont no about this time number tho
    Last edited by theonetrueace; Apr 17th, 2003 at 06:42 PM.

  14. #14

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    ok.....so % Processor Time isnt the right thing then......we need a Load,Usage, or something to that affect but i havent seen one of these all mornin.....no wait...its afternoon now...lol


    AcE

  15. #15
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, if you use .NextValue, you won't get that weird number... supposedly NextValue does some calculation that RawValue does not.... in any event, .NextValue solves the problem.

  16. #16

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    nope that aint it....already tryed it.....and just tryed again...it gives me 0 all the time

  17. #17
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, it worked for me... the counter must persist outside the sub, so it must be declared at the form level, and then when you click on a button, run code to read the NextValue....


    Anyway, I wrote something that will allow you to find out what categories are available ... its rather crude... there is a Process counter in there....

    VB Code:
    1. Dim f As PerformanceCounterCategory
    2.         Dim g As Int32
    3.         Dim t As String
    4.         Dim r As Array
    5.         r = System.Diagnostics.PerformanceCounterCategory.GetCategories()
    6.         For g = 0 To r.GetUpperBound(0)
    7.             f = (r.GetValue(g))
    8.             Console.WriteLine(f.CategoryName())
    9.  
    10.         Next

  18. #18

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    k that wuz the prob....i didnt have it declared outside the sub.....but now in the mist of all this i got another problem....i got an over flow ever when my progressbar value for my memory goes over 10%....wuts gonna be the best way to trouble shoot this?....bc it dont tell me where the problem is.....

    AcE

  19. #19

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    An unhandled exception of the type'System.OverflowException' occurred in system.windows.forms.dll

    Additional information: Arithmetic operation resulted in an overflow.

  20. #20
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    The progressbar probably takes a Int32, a type that the value returned by .NextValue doesn't fit into... you'll have to do truncate the numbers after the decimal... 5.3423423423 ....

    actually, performancecounter returns an Int64.... so it obviously won't fit...

  21. #21

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    naw this is seperate.....im doing
    cprogressbar1.refresh
    cprogressbar1.value = newprocess.workset

    and it works fine in a textbox so i put it in the progressbar and on 9% everytime.....boom...error....

    AcE

  22. #22
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yea well a textbox automatically converts the int64 into a string...and a string can hold as much data as your computer can... but an int32 which is the data type used by the progressbar.Value property can only use a 4-byte value... which an Int64 is not...

    use the convert class ... and make sure to have Option Strict checked in your IDE configuration so the compiler will warn you about these things...
    Last edited by nemaroller; Apr 17th, 2003 at 07:41 PM.

  23. #23

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    i commented the cpu calls out then used the convert call on the memory usage and still got the same error @ 10%

  24. #24
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    post ur code

  25. #25

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    ok.....finally got that fixed....im using a custom progressbar control and the problem wuz with the drawing of it....i just set it up to figure the percent before it got to the progressbar so it didnt get bombarded with crazy numbers.....

    next thing....do u think there is a _Average instancename or something to that affect....i tryed _Average but that doesnt work....reason im asking is because my processbar is jumping back and forth between 0 & 100 ... when the windows taskmanager says like 4 - 8 %.....

    AcE

    edit: nm.....changed some settings on the progressbar and all is well....this darn thing has been giving me fits all day....thx for ur help!
    Last edited by theonetrueace; Apr 18th, 2003 at 02:16 AM.

  26. #26
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    The taskmanager only polls from 1 second to every 5 seconds...(depending on the polling speed you set)

    Whereas a PerformanceCounter in a loop could be high as 100% CPU usage, but by the time the taskmanager gets around to reading the CPU load, it could have gone down to 4 or 1 %...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width