Pradeep Singh
Pradeep Singh

Reputation: 243

can a timer that fires continuously lead to memory leaks?

In my program i write data to files if i am not able to connect to database and I have a separate thread that has timer which checks for connection availability after every 25 seconds and if it can connect it transfers the data from files to the main db and deletes the file. the problem is i never stop this timer,can this lead to memory leaks? if i just run my program and monitor the task manager i can see the memory usage increasing continuously if i disable the timer and then run my application then the memory is stable

    public BackgroundWorker()
    {
        _backgroundWorkerThread = new Thread(new ThreadStart(ThreadEntryPoint));
        _timer = new SWF.Timer();
        _timer.Tick += new EventHandler(_timer_Tick);
        _timer.Interval = 25 * 1000;
        _timer.Enabled = true;
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        bool lanAvailabe = NetworkInterface.GetIsNetworkAvailable();
        if (lanAvailabe)
        {
            if (!GetListOfFiles())
            {
                return;
            }
        }
        else
            return;
    }

implementation of GetListofFiles()

    private bool GetListOfFiles()
    {
        string sourceDirectory = pathOfXmlFiles;
        if (!Directory.Exists(sourceDirectory))
        {
            return false;
        }
        var xmlFiles = Directory.GetFiles(sourceDirectory, "*.xml");
        if (!xmlFiles.Any())
        {
            return false;
        }
        foreach (var item in xmlFiles)
        {
            ReadXmlFile(item);
        }
        foreach (var item in xmlFiles)
        {
            if (_writtenToDb)
            {
                File.Delete(item);
            }
        }
        return true;
    }

method that reads xml files

    private void ReadXmlFile(string filename)
    {
        string[] patientInfo = new string[15];
        using (StreamReader sr = new StreamReader(filename, Encoding.Default))
        {
            String line;
            line = sr.ReadToEnd();
            if (line.IndexOf("<ID>") > 0)
            {
                patientInfo[0] = GetTagValue(line, "<ID>", "</ID>");
            }
            if (line.IndexOf("<PatientID>") > 0)
            {
                patientInfo[1] = GetTagValue(line, "<PatientID>", "</PatientID>");
            }
            if (line.IndexOf("<PatientName>") > 0)
            {
                patientInfo[2] = GetTagValue(line, "<PatientName>", "</PatientName>");
            }
            if (line.IndexOf("<Room>") > 0)
            {
                patientInfo[3] = GetTagValue(line, "<Room>", "</Room>");
            }

        }
        WriteToDb(patientInfo);
    }

Upvotes: 3

Views: 589

Answers (1)

Lukasz Madon
Lukasz Madon

Reputation: 14994

if i just run my program and monitor the task manager i can see the memory usage increasing continuously

Get a profiler. Task Manager is not a right tool. Can't tell what is going on. It doesn't mean that you have a leak. Maybe just GC doesn't run cause there is enough space etc.

Upvotes: 2

Related Questions