Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 503587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:24:33+00:00 2026-05-13T06:24:33+00:00

I’ve been profiling a method using the stopwatch class, which is sub-millisecond accurate. The

  • 0

I’ve been profiling a method using the stopwatch class, which is sub-millisecond accurate. The method runs thousands of times, on multiple threads.

I’ve discovered that most calls (90%+) take 0.1ms, which is acceptable. Occasionally, however, I find that the method takes several orders of magnitude longer, so that the average time for the call is actually more like 3-4ms.

What could be causing this?

The method itself is run from a delegate, and is essentially an event handler.

There are not many possible execution paths, and I’ve not yet discovered a path that would be conspicuously complicated.

I’m suspecting garbage collection, but I don’t know how to detect whether it has occurred.

Finally, I am also considering whether the logging method itself is causing the problem. (The logger is basically a call to a static class + event listener that writes to the console.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:24:34+00:00Added an answer on May 13, 2026 at 6:24 am

    As I commented, you really should at least describe what your method does, if you’re not willing to post some code (which would be best).

    That said, one way you can tell if garbage collection has occurred (from Windows):

    1. Run perfmon (Start->Run->perfmon)
    2. Right-click on the graph; select “Add Counters…”
    3. Under “Performance object”, select “.NET CLR Memory”
    4. From there you can select # Gen 0, 1, and 2 collections and click “Add”
    5. Now on the graph you will see a graph of all .NET CLR garbage collections
    6. Just keep this graph open while you run your application

    EDIT: If you want to know if a collection occurred during a specific execution, why not do this?

    int initialGen0Collections = GC.CollectionCount(0);
    int initialGen1Collections = GC.CollectionCount(1);
    int initialGen2Collections = GC.CollectionCount(2);
    
    // run your method
    
    if (GC.CollectionCount(0) > initialGen0Collections)
        // gen 0 collection occurred
    
    if (GC.CollectionCount(1) > initialGen1Collections)
        // gen 1 collection occurred
    
    if (GC.CollectionCount(2) > initialGen2Collections)
        // gen 2 collection occurred
    

    SECOND EDIT: A couple of points on how to reduce garbage collections within your method:

    1. You mentioned in a comment that your method adds the object passed in to “a big collection.” Depending on the type you use for said big collection, it may be possible to reduce garbage collections. For instance, if you use a List<T>, then there are two possibilities:

      a. If you know in advance how many objects you’ll be processing, you should set the list’s capacity upon construction:

      List<T> bigCollection = new List<T>(numObjects);

      b. If you don’t know how many objects you’ll be processing, consider using something like a LinkedList<T> instead of a List<T>. The reason for this is that a List<T> automatically resizes itself whenever a new item is added beyond its current capacity; this results in a leftover array that (eventually) will need to be garbage collected. A LinkedList<T> does not use an array internally (it uses LinkedListNode<T> objects), so it will not result in this garbage collection.

    2. If you are creating objects within your method (i.e., somewhere in your method you have one or more lines like Thing myThing = new Thing();), consider using a resource pool to eliminate the need for constantly constructing objects and thereby allocating more heap memory. If you need to know more about resource pooling, check out the Wikipedia article on Object Pools and the MSDN documentation on the ConcurrentBag<T> class, which includes a sample implementation of an ObjectPool<T>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 259k
  • Answers 259k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The following website (SS64.com) is INVALUABLE for batch script reference.… May 13, 2026 at 11:07 am
  • Editorial Team
    Editorial Team added an answer Are you talking about a database column of TIMESTAMP type??… May 13, 2026 at 11:07 am
  • Editorial Team
    Editorial Team added an answer Oh man, IE fun. I'm a mac person, so let's… May 13, 2026 at 11:07 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.