Re: Some TrackBar settings use excessive CPU and RAM



The TrackBar is a UI element, which means that it must calculate the drawing
of the TrackBar according to the ratio of the Value to the difference
between the Minimum and Maximum values. It must then factor in the size of
the Control, the various other environmental factors, such as how the
Control is resized according to the size of its container, the pixel density
of the screen, etc., and draw the bar in the location that corresponds to
that ratio of the computed screen size.

This involves the use of floating point arithmetic. As to whether the
arithmetic uses floats or doubles, I cannot say, but in either case, you're
talking about some expensive arithmetic, in terms of resource usage. The
larger the difference between the Minimum and Maximum, and factoring in the
LargeChange and SmallChange (the TickFrequency is less important), the
calculations consume more and more resources to perform the calculations,
which must be updated milliseconds apart (between each repaint of the
TrackBar while it is being moved).

The reason that the resources used are outside the Managed Heap is that the
TrackBar is using unmanaged resources under the hood, as do all
System.Windows.Forms.Components.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.


"Jeroen X. Pluimers" <jeroen.news.xs4all.nl@xxxxxxxxxxxx> wrote in message
news:456d882f$0$321$e4fe514c@xxxxxxxxxxxxxxxxx
Some TrackBar settings use excessive CPU and RAM.

How to reproduce:

1. In VS 2005 or VS 2003, Create a new WinForms app (the code below is C#,
but it works in any .NET language)
2. Drop a button and a trackbar component on the form.
3. Double click on the button, and enter this code:
trackBar1.Minimum = 0;
/// Uncomment the lines below, and your high CPU usage goes
down:
//trackBar1.LargeChange = 10000000;
//trackBar1.TickFrequency = 1000000;
trackBar1.Maximum = 38389344; // this line uses up 150-megs of
RAM outside the .NET heap!
4. Run the app, and observe the memory usage in the task manager (it uses
about 10 megs of RAM)
5. Click on the button: RAM usage for the app goes to 160 megs
immetiately, followed by a couple of seconds of high CPU-usage
6. quit the app
7. Uncomment these two lines in the code:
trackBar1.LargeChange = 10000000;
trackBar1.TickFrequency = 1000000;
8. Run the app, and observe the memory usage in the task manager (it uses
about 10 megs of RAM)
9. Click on the button: RAM usage for the app goes to 160 megs
immetiately, but no high CPU usage

The memory used is outside the .NET heap.
It fails on both English and Dutch Windows XP versions with SP2 installed.

If you make Maxium smaller, you get similar results:

Maximum: Memory usage delta
38389344: 150 megs
3838934: 15 megs
383893: 1,5 megs

Roughly, it looks like the TrackBar uses 4 bytes each time Maximum
increased by one.

Why?


Basically, it makes the TrackBar unusable for large Maximum values.

How to work around that?


Regards

--jeroen



.