DotNetKick.com is an open-source project. Please report any bugs and let us know your great suggestions. Currently running svn revision 620 (rss)

Kick Spy!, Kick Zeitgeist and Kick Widgets

18
kicks
published 1 year, 8 months ago, submitted by cls2deg cls2deg 1 year, 8 months ago

diditwith.net — One of the greatest frustration of working with delegates and events is that they can potentially cause memory leaks if they aren't unhooked. In this article, we will solve this problem in a variety of ways to get the best performance, memory use and syntax.

Add a comment 2 comments | category: | Views: 43 | Get KickIt image code
tags: , , , | tag it

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:

Comments:
Article is a better resource for the inner workings of the CLR and GC than for preventing event-related memory leaks.
posted by yesthatmcgurk yesthatmcgurk 1 year, 8 months ago
Hello Dustin.
Weak events, or should I say the lack of them causing me headache as well.
That's why I was extremely pleased to stumble upon your excellent blog. However, I got different measurements, which make using LCG a very good alternative only slightly slower than using unbound delegates:
[normal]:
Subscribing one event handler takes about 0.55988 microseconds
Firing one event handler takes about 0.39475 microseconds
[weak by reflection]:
Subscribing one event handler takes about 19.2845 microseconds
Firing one event handler takes about 19.66328 microseconds
Subscribing is 34.4440 times slower
Firing is 49.8120 times slower
[weak by LCG]:
Subscribing one event handler takes about 91.05281 microseconds
Firing one event handler takes about 0.57962 microseconds
Subscribing is 162.6292 times slower
Firing is 1.4683 times slower
[weak by unbound delegate]:
Subscribing one event handler takes about 38.31363 microseconds
Firing one event handler takes about 0.56384 microseconds
Subscribing is 68.4319 times slower
Firing is 1.4283 times slower

I subscribe and fire 10000 times and use the Stopwatch class to measure the times.

Here is how I measure the times:

public double Subscribe(int count)
{
EventProvider provider = new EventProvider();
Stopwatch sw = new Stopwatch();
for (int i = 0; i < count; ++i)
{
sw.Start();
provider.MyEvent += new WeakEventHandler<EventArgs>(MyEventHandler);
sw.Stop();
provider.Reset(); // clears the event delegate
}
return (1000 * sw.Elapsed.TotalMilliseconds) / count;
}

public double Fire(int count)
{
EventProvider provider = new EventProvider();
provider.MyEvent += new WeakEventHandler<EventArgs>(MyEventHandler);

Stopwatch sw = new Stopwatch();
for (int i = 0; i < count; ++i)
{
provider.Fire(sw);
}
return (1000 * sw.Elapsed.TotalMilliseconds) / count;
}

Am I doing something wrong or is LCG indeed a very good option?
Thanks.
posted by markell 9 months, 25 days ago



information Login or create an account to comment on this story
 

Sponsored Link: www.carlist.ie

Search:

Ads via The Lounge