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

The Archive Base Latest Questions

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

In a windows service I know I have a memory leak. How I know

  • 0

In a windows service I know I have a memory leak. How I know is outside the scope of this question but you can see the initial question here

I have a windows service with some classes:

public partial class VLSService : ServiceBase
{

   ReportedContentProcess reportedContent;

   protected override void OnStart(string[] args)
   {
        //when a user reports a video
        reportedContent = new ReportedContentProcess();
        reportedContent.ProcessTimer.Elapsed += new ElapsedEventHandler(ReportedContentTimer_Elapsed);

   }

   void ReportedContentTimer_Elapsed(object sender, ElapsedEventArgs e)
   {
        reportedContent = new ReportedContentProcess();
        reportedContent.Process();
        reportedContent.ProcessReinstated();
   }

}


public class ReportedContentProcess : ProcessBase
    {

        //location on the library
        string libraryArchivedFilePath;
        string libraryRealHiFilePath;
        string libraryRealLoFilePath;
        string libraryFlashHiFilePath;
        string libraryFlashLoFilePath;

        //location on the reported folder
        string reportedContentArchivedFilePath;
        string reportedContentRealHiFilePath;
        string reportedContentRealLoFilePath;
        string reportedContentFlashHiFilePath;
        string reportedContentFlashLoFilePath;

        string reportedContentFolderPath;
        static EmailSettings emailSettings;

        /// <summary>
        /// This process will move reported content out of the 'real' and 'flash' mounted folders on the 
        /// hard drive/ storeage location so they cannot under any circumstances be got to by any users
        /// of the library.
        /// </summary>
        public ReportedContentProcess(): base(1021)
        {
            DirectoryInfo arciveFolderPathInfo = new DirectoryInfo(fileSystemReferencesForService.ArchiveDir);
            DirectoryInfo contentFolder = arciveFolderPathInfo.Parent;
            reportedContentFolderPath = contentFolder.FullName.ToString() + @"\ReportedContent\";
            emailSettings = settingsManagerForService.GetEmailSettings();
        }

        public override void Process()
        {

            if (!EnumsAndConstants.ApplicationLocks.ReportedContentProcessRunning)
            {

                EnumsAndConstants.ApplicationLocks.ReportedContentProcessRunning = true;

                videosToProcess = contentManagerForService.GetReportedVideos(false);

                //get the reportedvideo object for this video
                CreateReportedVideoContentFolder();

                ReportedVideo reportedVideo;

                foreach (Video v in videosToProcess)
                {

                    string flashVideoExt = string.Empty;

                    if (v.IsAudio)
                    {
                        flashVideoExt = ".mp3";
                    }
                    else
                    {
                        flashVideoExt = ".mp4";
                    }

                    //library location of each file for video
                    libraryArchivedFilePath = fileSystemReferencesForService.ArchiveDir + v.LocalFile;

                    libraryRealHiFilePath = fileSystemReferencesForService.RealDir + v.Url.ToString() + "_hi.rm";
                    libraryRealLoFilePath = fileSystemReferencesForService.RealDir + v.Url.ToString() + "_lo.rm";
                    libraryFlashHiFilePath = fileSystemReferencesForService.FlashDir + v.Url.ToString() + "_hi" + flashVideoExt;
                    libraryFlashLoFilePath = fileSystemReferencesForService.FlashDir + v.Url.ToString() + "_lo" + flashVideoExt;

                    //new location for file to go to
                    reportedContentArchivedFilePath = reportedContentFolderPath + v.LocalFile;


}

}


/// <summary>
/// A base class that holds all the Global objects for any process that operates under the 
/// service. This process works with 
/// </summary>
public abstract class ProcessBase
{
    public Timer processTimer;
    public Timer ProcessTimer{get{ return processTimer;}set{processTimer=value;}}

    protected SqlConnection connection;
    protected VlsContent contentManagerForService;
    protected VlsSecurity securityManagerForService;
    protected VlsSettings settingsManagerForService;
    protected FileSystemReferences fileSystemReferencesForService;
    protected List<Video> videosToProcess;
    protected ExeReferences exeReferenecesForService;
    protected GeneralSettings generalSettingsForService;

    public abstract void Process();


//sets up all the common objects
public ProcessBase() 
{

    connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Db"].ToString());
    contentManagerForService = new VlsContent(connection);
    settingsManagerForService = new VlsSettings(connection);
    securityManagerForService = new VlsSecurity(connection);
    fileSystemReferencesForService = settingsManagerForService.GetFileSystemReferences();
    exeReferenecesForService = settingsManagerForService.GetExeReferences();
    generalSettingsForService = settingsManagerForService.GetGeneralSettings();

}


//This constructor will call the default constructor ^
protected ProcessBase(long intervalArg) : this()
{
    processTimer = new Timer(intervalArg);
    processTimer.Enabled = true;

}

}

After profiling this code it seems that this is causing a memory leak. What im wondering is why?

Im thinking that the problematic line is:

reportedContent = new ReportedContentProcess(); [located in the event handler]

But I cant really see why. Surely it will creat pointer in memory called ‘reportedContent’ then when the above is called it will place actual value on the heap with new values for the members of ReportedContentProcess(). Then when the event handler is run again after about 1 second it will then just replace the GC root pointer ‘reportedContent’ with a new allocated heap item for the ReportedContentProcess() class. Then the old one (and all of its now abandoned child objects will be garbaged collected as their root is no longer referenced by the call stack..? This should just happen over and over again (out with the old in with the new) style.

Hope some can help I sort of hope this is the problem so I can fix it but want to check before I start re-factoring code.

The profile is here:

enter image description here

  • 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-23T09:29:54+00:00Added an answer on May 23, 2026 at 9:29 am

    I don’t know what ProcessBase is but I am thinking it is where your problem is.

    Does it deal with anything outside of the .NET/C# environment? Should it implement an IDisposable interface and should the existing ReportedProcess be disposed before a new one is created.

    Also, you create an event handler to the initial ReportedProcess in OnStart(). You then lose a reference to this with the first event by assigning a new instance to its reference, however the initial ReportedProcess is kept alive by the fact that it has a contained object that has a subscribed event handler, it will not be garbage collected until the end of the programme and is technically a memory leak.

    If somewhere in the ReportedProcess class you apply the same strategy with regards to cleaning up event handlers then there is your problem.

    ** UPDATED ANSWER **

    Just looking at your updated code, it’s missing ProcessReinstated() code, but I think it maybe the fact that you do not explicitly Close your SqlConnection. Your ProcessBase should implement an IDisposable interface and your timer code should explicitly dispose of existing ReportedProcess classes before instantiating new ones, this is the best strategy with things like Database connections and sockets and other external connections.

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

Sidebar

Related Questions

I know how I can create a Windows Service in C# but I have
I know how to attach a debugger to a windows service, but I can't
I have the service name for a windows service in delphi, and I know
I am creating a windows service and want to know best practices for this.
I have a windows service executable that I know is written in .NET which
I have a windows service written around some code similar to this Asynchronous Server
I have a windows service which OnStart calls a Webservice and this webservice in
I have created a windows service which is set to start automatically. This service
I have a problem with memory leaks in my .NET Windows service application. So
I have checked all posts here, but can't find a solution for me so

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.