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 1077301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:33:17+00:00 2026-05-16T21:33:17+00:00

I have some code in a thread. The code’s main function is to call

  • 0

I have some code in a thread. The code’s main function is to call other methods, which write stuff to a SQL database like this:

private void doWriteToDb()
    {
        while (true)
        {
            try
            {
                if (q.Count == 0) qFlag.WaitOne();

                PFDbItem dbItem = null;
                lock (qLock)
                {
                    dbItem = q.Dequeue();
                }
                if (dbItem != null)
                {
                    System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
                    //write it off
                    PFResult result = dbItem.Result;
                    double frequency = dbItem.Frequency;
                    int i = dbItem.InChannel;
                    int j = dbItem.OutChannel;
                    long detail, param, reading, res;
                    detail = PFCoreMethods.AddNewTestDetail(DbTables, _dicTestHeaders[result.Name.ToString().ToLower()]);

                    param = PFCoreMethods.AddNewTestParameter(DbTables, detail, "Frequency");
                    PFCoreMethods.AddNewTestParameterValue(DbTables, param, frequency.ToString());

                    param = PFCoreMethods.AddNewTestParameter(DbTables, detail, "In channel");
                    PFCoreMethods.AddNewTestParameterValue(DbTables, param, i.ToString());

                    param = PFCoreMethods.AddNewTestParameter(DbTables, detail, "Out channel");
                    PFCoreMethods.AddNewTestParameterValue(DbTables, param, j.ToString());

                    param = PFCoreMethods.AddNewTestParameter(DbTables, detail, "Spec");
                    PFCoreMethods.AddNewTestParameterValue(DbTables, param, result.Spec);


                    dbItem.Dispose();
                    dqcnt++;
                    sw.Stop();
                }
            }
            catch (Exception ex)
            {


            }

        }
    }

The AddNewTestParameter method is using a 3rd party class which has the SQL code. Currently I have no access to its internals.

DbTables is a collection object, whose properties are table objectscreated by the 3rd party program. There is only one DbTable object which the program uses.

The problem is that as time passes (couple of hours) the AddNewTestParameter method call takes longer and longer, starting from about 10ms to about 1sec.

The q, is a queue with objects that contain the necessary information to write into the database. The items are added to this queue by the main thread. THe thread simply takes them out, writes them, and disposes of them. The q.Count is no more than 1, although in time as the database writes become slower, the q.Count rises since dequeueing cannot catch up. At its worst, the q.Count was over 30,000. I write over 150,000 entries to the database in total.

On the SQL end, I ran some traces on the server, and the trace shows that internally SQL always takes about 10ms, even during the time the C# code itself takes 1sec.

So, currently, I have 2 suspicions:

  1. My code is the problem. The thread is low-priority, perhaps this might affect performance. Also, after watching the memory usage for 20 minutes, I see that it rises at about 100K/min, CPU usage seems constant around %2-5. How can I figure out where the memory leak happens? Can I pinpoint it to a specific part of the code?

  2. The 3rd party code is the problem. How could I go about proving this? What methods are there to watch and confirm that the problem lies in 3rd party code?

  • 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-16T21:33:17+00:00Added an answer on May 16, 2026 at 9:33 pm

    Anyway, if I had to make a suggestion I would look at DBTables … if that’s a collection maybe you’re forgetting to reset it so everytime you call it it has one more element… so after a while the 3rd party routine that’s O(n^2), or something like that, starts to degrade because it’s expecting a worst case scenario of 20 tables and you’re providing 1000.

    Edit: Ok, I would discard the problem being in the queue as dequeuing should be a really fast operation (you can measure it anyway). It still points to the DBTables collection growing bigger and bigger, have you check its size after the first x iterations?

    Edit2: Ok, another approach, let’s say the AddNewTestParameter does exactly what is says it does… ADD a new parameter that then gets added to an internal collection. Now, there are two options, if that’s the case, either you’re supposed to clear that collection by calling the “ClearParameters” function after each iteration and then it will be your fault, or you have not such functionality and then it’s 3rd code fault. That would explain also your memory loses (altough that can also be related to the growing queue)

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

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • 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 Rails 3.2 first introduced first_or_create to ActiveRecord. Not only does… May 17, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer Thank you Wrikken, based on your advice I came up… May 17, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer If I understand correctly you have a footer on every… May 17, 2026 at 1:07 am

Trending Tags

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

Top Members

Related Questions

I have some parallel Fortran90 code in which each thread needs to generate the
Suppose I have some code which is running in the UI thread, which spawns
I have to execute some code in the context of the main thread. I
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code where I use a thread static object in C#. [ThreadStatic]
Let's say we have some code like this running in the separate thread: private
I have some code which is been running by a backgroundworker I'd like some
I have some java code that gets and sets a session attribute: Object obj
I have some code that I want to only allow access to by one
I have some code that will be accessed from two threads: class Timer{ public:

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.