C# updating notifyicon increases memory consumption

C# updating notifyicon increases memory consumption
typescript
Ethan Jackson

Have C# method call every second, which updates notifyicon icon.
And memory consumption grows every update.

My code:

private void UpdateTrayIcon() { var info = GetRecycleBinInfo(); try { notifyIcon.Icon = info.ItemCount == 0 ? Properties.Resources.tray_empty : Properties.Resources.tray_full; } catch (Exception ex) { MessageBox.Show("Error setting icon!"); } }

When notifyIcon.Icon line is commented out - no memory usage growth.

Any suggestions? What am I doing wrong?

Previously it was updated by new System.Drawing.Icon(iconpath) and it was same.

Answer

Found answer:

First initialize them on form load, or creation, or whatever:

iconFull = (Icon)Properties.Resources.tray_full.Clone(); iconEmpty = (Icon)Properties.Resources.tray_empty.Clone();

And then

notifyIcon.Icon = info.ItemCount == 0 ? iconEmpty : iconFull;

Related Articles