Question

I have an application running constantly on my server.

I start off a thread which goes into an infinite loop.

The 1st thing this loop does is enumerate directories in a certain folder.

This folder has many sub-folder that receives small image files from many clients.

My loop looks for any directories/files that are older than say 24hrs from today's immediate time /date.

If it is older then my code deletes the files and the parent folder.

My loop will then 'sleep' for 60 seconds before repeating again.

Now, what I have noticed is that if there is a large number of files to delete or/and the app has been running for several days the explorer.exe memory increases significantly.

So, I have to question my code.

This is my code:

Thread thArchiver = null;
private void btnArchiver_Click(object sender, EventArgs e)
{
    try
    {
        if (btnArchiver.Text == "Start")
        {
            btnArchiver.Text = "Stop";
            lvwServices.Items[4].SubItems[1].Text = "Started";
            thArchiver = new Thread(ArchiveWorker);
            thArchiver.Start();
        }
        else
        {
            btnArchiver.Text = "Start";
            lvwServices.Items[4].SubItems[1].Text = "Stopped";
            thArchiver.Abort();

        }

    }
    catch { }
}

 private void ArchiveWorker()
 {
     while (true)
     {
         try
         {
             List<string> catDirs = Directory.EnumerateDirectories(CataloguePath, "*", SearchOption.TopDirectoryOnly).ToList();
             for (int index = 0; index < catDirs.Count; index++)
             {
                 //look at time of creation and then delete the directory
                 Directory.Delete(catDirs[index]);
             }
         }                     
         catch (Exception _ex)
         {
          //report the error
         }
         Thread.Sleep(60000);
     }
 }
Was it helpful?

Solution

This is very much possible!

Reasons can be as your thread sleeps for 60 sec only

List<string> catDirs = Directory.EnumerateDirectories(CataloguePath, "*", SearchOption.TopDirectoryOnly).ToList();

And you are creating list in every new thread so what is happening is that the list of folders and its reference is stored again and again. And this is a never ending process so it will keep going on and on and size increases. This get stored in memory so you see this lag.

what I suggest is: Don't open new thread and put a check that last thread is completed or not.

Update: Possible reason as per comments by @Andrew and @Maarten

List<string> catDirs = Directory.EnumerateDirectories(CataloguePath, "*", SearchOption.TopDirectoryOnly).ToList();
             for (int index = 0; index < catDirs.Count; index++)
             {
                 //look at time of creation and then delete the directory
                 Directory.Delete(catDirs[index]);
             }

I feel the Forloop is taking time because what might be happening is Directory.Delete(catDirs[index]); starts a new thread itself no what might be happening is there is lot of files to get deleted in queue and queue increases day by day. Thats might be the reason you are seeing lag

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top