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
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.

Signature
HTH,
Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com
Parabola is a mate of plane.
> Some TrackBar settings use excessive CPU and RAM.
>
[quoted text clipped - 46 lines]
>
> --jeroen
Jeroen X. Pluimers - 29 Nov 2006 15:28 GMT
>> The memory used is outside the .NET heap.
>> It fails on both English and Dutch Windows XP versions with SP2
[quoted text clipped - 9 lines]
>> Roughly, it looks like the TrackBar uses 4 bytes each time Maximum
>> increased by one.
>> Why?
> 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).
Just found out that for the memory usage, LargeChange and SmallChange do NOT
matter:
private void button1_Click(object sender, System.EventArgs e)
{
trackBar1.Minimum = 0;
trackBar1.SmallChange = 10000;
trackBar1.LargeChange = 100000;
trackBar1.Maximum = 1000000;
}
The about code uses about 4 megabytes of memory, as per calculations above.
So it looks like the TrackBar is trying to improve performance by using an
array of doubles (they are 4 bytes each) per 1-increment of the range.
Cutting a long story short: it makes TrackBar a no-go for large and/or
precise ranges, which makes my second question very relevant:
>> Basically, it makes the TrackBar unusable for large Maximum values.
>> How to work around that?
Regards,
--jeroen
Kevin Spencer - 30 Nov 2006 12:53 GMT
Sorry I overlooked the second question Jeroen.
I would simply make the range smaller, by dividing the total amount by some
number. Then, you can use increments of that number to set the Value of the
TrackBar.
For example, if your Difference is 38389344, you might divide by 1000, and
round down:
38389
Note that your difference is still larger than the number of pixels that
will show in the TrackBar while it is tracking. Any number larger than that
would not impact the appearance of the TrackBar. Then, when your counting
mechanism is increased by a multiple of 1000, the Value increments by 1.

Signature
HTH,
Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com
Parabola is a mate of plane.
>>> The memory used is outside the .NET heap.
>>> It fails on both English and Dutch Windows XP versions with SP2
[quoted text clipped - 43 lines]
>
> --jeroen
Jeroen X. Pluimers - 18 Dec 2006 08:48 GMT
> Sorry I overlooked the second question Jeroen.
No problem. In the meantime I had taken a similar approach: having a
trackbar with Min=0 and a low Max (currently 100, since it is larger than
the number of pixels in the trackbar) and then extrapolating to/from the
real value.
It needed some magic when to (or not to) update from/to the extrapolated
value, but it works.
Regards,
--jeroen