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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:22:23+00:00 2026-05-30T09:22:23+00:00

I’m constantly (every 30-60 min) getting a System.OutOfMemoryException in my Windows Service. The service’s

  • 0

I’m constantly (every 30-60 min) getting a System.OutOfMemoryException in my Windows Service. The service’s job is to loop though 6 directories which contains data files which the service datawashes to a common XML data format.

These 6 folders contains 5-10.000 files each, so the total number of files is about 45.000 and new files is added duing the day. There is added about 1-2000 new files a day. The files is between 4KB and 500KB.

Each data file is washed to the common XML data format through the XElement object.

I have used RedGates ANTS Memory Profiler on the service and the objects which are using the most memory is string (about 90.000.000 bytes) and XElement (about 51.000.000 bytes).

In the Memory Profiler, when i trace, what is using the string object, i can see that it’s mostly (93%) the XElement object which is using the string object.

The server have 6 cpu’s and 6GB of RAM, so i can’t see why i’m getting the OutOfMemoryException. If i look at the Windows Service in the Processes it’s MAX use of RAM have been 1.2GB.

I have read that .NET garbage collector doesn’t clear the string object because the string object is stored in a intern table. Could this be the error, if so what can i do about it?

The code below shows how i’m looping through the files. As you can see i have also tried to take 20 files at a time. This just pushes the OutOfMemoryException a few hours, so the service will run for 4-5 hours instead of 30-60 min.

Why do i can the OutOfMemoryException?

private static void CheckExistingImportFiles(object sender, System.Timers.ElapsedEventArgs e)
    {
        CheckTimer.Stop();
        var dir = Directory.GetFiles(RawDataDirectory.FullName, "*.*", SearchOption.AllDirectories);

        List<ManualResetEvent> doneEvents = new List<ManualResetEvent>();
        int i = 0;
        //int doNumberOfFiles = 20;

        foreach (string existingFile in Directory.GetFiles(RawDataDirectory.FullName, "*.*", SearchOption.AllDirectories))
        {
            if (existingFile.EndsWith("ignored") || existingFile.EndsWith("error") || existingFile.EndsWith("importing"))
            {
                //if (DateTime.UtcNow.Subtract(File.GetCreationTimeUtc(existingFile)).TotalDays > 5)
                //  File.Delete(existingFile);
                //continue;
            }

            StringBuilder fullFileName = new StringBuilder().Append(existingFile);

            if (!fullFileName.ToString().ToLower().EndsWith("error") && !fullFileName.ToString().ToLower().EndsWith("ignored") && !fullFileName.ToString().ToLower().EndsWith("importing"))
            {
                File.Move(fullFileName.ToString(), fullFileName + ".importing");
                fullFileName = fullFileName.Append(".importing");

                ImportFileJob newJob = new ImportFileJob(fullFileName.ToString());

                doneEvents.Add(new ManualResetEvent(false));

                ThreadPool.QueueUserWorkItem(newJob.Run, doneEvents.ElementAt(i));
                i++;
            }

            //if (i > doNumberOfFiles)
            //{
            //    i = 0;
            //    doNumberOfFiles = 20;
            //    break;
            //}
        }
        i = 0;
        WaitHandle.WaitAll(doneEvents.ToArray());

        CheckTimer.Start();
    }
  • 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-30T09:22:25+00:00Added an answer on May 30, 2026 at 9:22 am

    As Avner Shahar-Kashtan already stated, I also think that the problem is in ImportJob(you haven’t shown us its code).

    Even so, you can still make some optimizations.

    You don’t have to load all file names at once. It can be done dir by dir as below

    IEnumerable<string> GetAllFiles(string dirName)
    {
        var dirs = Directory.GetDirectories(dirName);
    
        foreach (var file in Directory.GetFiles(dirName))
            yield return file;
    
        foreach (var dir in dirs) //recurse
            foreach (var file in GetAllFiles(dir)) 
                yield return file;
    }
    

    And by using TPL, you can reduce the number of ManualResetEvents created (and their forgotten Dispose()s)

    Parallel.ForEach(GetAllFiles(RawDataDirectory.FullName) , file =>
    {
        //ImportFileJob newJob = new ImportFileJob(file);
        //newJob.Run
        Console.WriteLine(file);
    }); 
    

    BTW, you should also see CountdownEvent

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to loop through a bunch of documents I have to put

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.