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

  • SEARCH
  • Home
  • 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 7031495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:48:38+00:00 2026-05-28T00:48:38+00:00

Hi and thanks for looking! Background I have a computing task that requires either

  • 0

Hi and thanks for looking!

Background

I have a computing task that requires either a lot of time, or parallel computing.

Specifically, I need to loop through a list of about 50 images, Base64 encode them, and then calculate the Levenshtein distance between each newly encoded item and values in an XML file containing about 2000 Base64 string-encoded images in order to find the string in the XML file that has the smallest Lev. Distance from the benchmark string.

A regular foreach loop works, but is too slow so I have chosen to use PLINQ to take advantage of my Core i7 multi-core processor:

Parallel.ForEach(candidates, item => findImage(total,currentWinner,benchmark,item));

The task starts brilliantly, racing along at high speed, but then I get an "Out of Memory" exception.

I am using C#, .NET 4, Forms App.

Question

How do I tweak my PLINQ code so that I don’t run out of available memory?

Update/Sample Code

Here is the method that is called to iniate the PLINQ foreach:

private void btnGo_Click(object sender, EventArgs e)
{
    XDocument doc = XDocument.Load(@"C:\Foo.xml");
    var imagesNode = doc.Element("images").Elements("image"); //Each "image" node contains a Base64 encoded string.
    string benchmark = tbData.Text; //A Base64 encoded string.
    IEnumerable<XElement> candidates = imagesNode;

    currentWinner = 1000000; //Set the "Current" low score to a million and bubble lower scores into it's place iteratively.
    
    Parallel.ForEach(candidates, i => {
        dist = Levenshtein(benchmark, i.Element("score").Value);
        if (dist < currentWinner)
        {
            currentWinner = dist;
            path = i.Element("path").Value;
        }
    });
}

. . .and here is the Levenshtein Distance Method:

public static int Levenshtein(string s, string t)    {
            int n = s.Length;
            int m = t.Length;
            var d = new int[n + 1, m + 1];

            // Step 1
            if (n == 0)
            {
                return m;
            }

            if (m == 0)
            {
                return n;
            }

            // Step 2
            for (int i = 0; i <= n; d[i, 0] = i++)
            {
            }

            for (int j = 0; j <= m; d[0, j] = j++)
            {
            }

            // Step 3
            for (int i = 1; i <= n; i++)
            {
                //Step 4
                for (int j = 1; j <= m; j++)
                {
                // Step 5
                int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                // Step 6
                d[i, j] = Math.Min(
                    Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
                }
            }
            // Step 7
            return d[n, m];
            }

Thanks in advance!

  • 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-28T00:48:38+00:00Added an answer on May 28, 2026 at 12:48 am

    Update

    Ran into this error again today under different circumstances. I was working on a desktop app with high memory demand. Make sure that you have set the project for 64-bit architecture to access all available memory. My project was set on x86 by default and so I kept getting out of memory exceptions. Of course, this only works if you can count on 64-bit processors for your deployment.

    End Update

    After struggling a bit with this it appears to be operator error:

    I was making calls to the UI thread from the parallel threads in order to update progress labels, but I was not doing it in a thread-safe way.

    Additionally, I was running the app without the debugger, so there was an uncaught exception each time the code attempted to update the UI thread from a parallel thread which caused the overflow.

    Without being an expert on PLINQ, I am guessing that it handles all of the low-level allocation stuff for you as long as you don’t make a goofy smelly code error like this one.

    Hope this helps someone else.

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

Sidebar

Related Questions

Hi and thanks for looking! Background I have a workflow that constructs a set
Thanks for looking on this problem. I have a page that is totally valid
Hi and thanks for looking! Background I have inherited an old .NET project based
Hello and thanks for looking! Background I currently have a C# method for looping
Hi and thanks for looking! Background I have made a simple little app in
Hi and thanks for looking! Background I have been developing .NET Web applications for
Hi and thanks for looking! Background I need to pull the file locations (path
Hello and thanks for looking! Background I am designing a greenfield application using .NET4
Hi and thanks for looking! Background This is an extension on this question: How
I have a CSS class that makes my links look like nice looking buttons.

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.