Memory mgmt issue
Memory mgmt issue
Memory mgmt issue
Memory mgmt issue
Memory mgmt issue
Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue
Memory mgmt issue Memory mgmt issue
Memory mgmt issue
Go Back  Xtreme Visual Basic Talk > > > Memory mgmt issue


Reply
 
Thread Tools Display Modes
  #1  
Old 12-29-2013, 12:27 PM
Mike Mike is offline
Contributor
 
Join Date: Apr 2001
Location: Montreal, Canada
Posts: 462
Default Memory mgmt issue


Hi folks,

I have a service running 24/7/365 on client machines. Every 60 seconds, it loops through the following:

Code:
        Dim conDB As SqlConnection = Nothing
        Dim bPerformBackup As Boolean = False
        Dim adaBackupScan As SqlDataAdapter = Nothing
        Dim dstBackupScan As DataSet = Nothing
        Dim cmd As SqlCommand = Nothing
        Dim bConnectionProblem As Boolean = False
        Dim strQuery As String = ""


        Call WriteToLog("Checking for backup requirement.")

        Try
            conDB = New SqlConnection(ConnString("BackupsSQL"))
            conDB .Open()
        Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try

        If conDB.State = ConnectionState.Open Then
            adaBackupScan = New SqlDataAdapter
            dstBackupScan = New DataSet
            Try
                strQuery = "Select TOP 1 * From Backups where Req = 1 Order by BackupTime ASC"
                cmd = New SqlCommand(strQuery, conDB)
                adaBackupScan.SelectCommand = cmd
                adaBackupScan.Fill(dstBackupScan, "AutoBackups")
            Catch ex As Exception
                Call WriteToLog(ex.ToString)
                bConnectionProblem = True
            End Try

            If Not bConnectionProblem Then
                Try
                    If dstBackupScan.Tables("AutoBackups").Rows.Count > 0 Then
                            bPerformBackup = True
                    End If
                Catch ex As Exception
                    Call WriteToLog(ex.ToString)
                End Try

                If bPerformBackup Then Call BackUpFiles
            End If
        End If

        Try
            dstBackupScan.Clear()
        Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try

        Try
            If Not conDB.State = ConnectionState.Closed Then conDB.Close()
        Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try

        Try
            If Not (cmd Is Nothing) Then cmd.Dispose()
            If Not (dstBackupScan Is Nothing) Then dstBackupScan.Dispose()
            If Not (adaBackupScan Is Nothing) Then adaBackupScan.Dispose()
            If Not (conDB Is Nothing) Then conDB.Dispose()
        Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try

        Try
            System.GC.Collect()
        Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try

    End Sub

    Public Sub WriteToLog(ByVal strEntry As String)
        Try
            Using w As System.IO.StreamWriter = System.IO.File.AppendText(DataPath & "\AutoLog.log")
                w.WriteLine("{1} {0} : {2}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString(), strEntry )
            End Using
        Catch ex As Exception
            'no action
        End Try
    End Sub
My issue is that the first time that this routine is called, memory usage jumps when I look at the process stats in the Windows Task Manager. It does not get released by initiaing the GC. However, as the code gets called every 60 seconds, memory usage only increases slightly afterwards. The same effect happens with pretty much all my other routines.

Normally, I don't initiate the GC but I thought of doing so in order to test its effects.

My question is: is this behavior actually normal or am I forgetting to do something for the memory used by the queried data and the connection to be released?

Thanks!

Mike
Reply With Quote
  #2  
Old 01-02-2014, 08:04 PM
PlausiblyDamp's Avatar
PlausiblyDampMemory mgmt issue PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Memory in use by your application and memory allocated to your application can be different things.

If windows has allocated memory to your application then it might not be released back to windows immediately simply because there is a good chance you will need the memory again soon anyway. This can account for the behaviour you are seeing - memory allocated isn't dropping but neither is it increasing appreciably.

If the memory is constantly increasing however then this can be a sign of a memory leak somewhere....

One thing I would suggest is to let .Net do as much work for you as possible, currently you are taking responsibility for the connection objects, command objects, data adapter objects and data set object yourself. Every one of these is a chance for you to miss something that needs freeing up.

A SqlDataAdapter has an overload that takes a command as a string and a connection string (http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx). If you use that then it will take care of creating and disposing of commands and connections for you - this means you could simplify the resource management by wrapping your DataAdapter in a single using block or a single try ... catch.

Removing some of the If ... End If statements could also simplify the code, if you are already wrapping things in a Try ... Catch block then you may as well just see if things work and deal with failures rather than doing a lot of up front checks as well as handling errors.

As an aside DataSets don't really need to be disposed of either as at runtime they don't use any non-managed resources.

As an example something like
Code:
Dim conDB As SqlConnection = Nothing
Dim bPerformBackup As Boolean = False
Dim adaBackupScan As SqlDataAdapter
Dim dstBackupScan As DataSet     
       
Try
	Dim adaBackupScan as New SqlDataAdapter(ConnString("BackupsSQL"), "Select TOP 1 * From Backups where Req = 1 Order by BackupTime ASC")       
	adaBackupScan.Fill(dstBackupScan, "AutoBackups")
        If dstBackupScan.Tables("AutoBackups").Rows.Count > 0 Then
        	BackUpFiles
        End If        
 
        dstBackupScan.Clear()
               
	If Not (adaBackupScan Is Nothing) Then adaBackupScan.Dispose()     

 	Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try  
  
        Try
            System.GC.Collect()
        Catch ex As Exception
            Call WriteToLog(ex.ToString)
        End Try
End Sub
might be a simpler way of doing things.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 01-06-2014, 08:44 PM
Mike Mike is offline
Contributor
 
Join Date: Apr 2001
Location: Montreal, Canada
Posts: 462
Default

Thanks PlausiblyDamp,

Your post is an eye-opener for me. It also alleviates a lot of concerns regarding memory leaks.

But w.r.t. the Try-Catch-EndTry statements, because my knowledge of every object is so underwhelming (and the fact that this is a service which runs 24/7/365), I'm afraid that if I omit to put a Try-Catch-EndTry somewhere, the service might stall upon hitting an unmanaged exception. This is the type of thing that unfortunately keeps me up at night. For instance, in the example that you supplied, if an exception occurs on dstBackupScan.Clear(), although the service will not stall because the whole routine is wrapped in a try statement, then I figure that the adaBackupScan.Dispose statement would be skipped. Could the dstBackupScan.Clear() ever really encounter an exception? Perhaps not, but given that I'm not sure about that sort of stuff, I wrap it just to be on the safe side...
Reply With Quote
  #4  
Old 01-07-2014, 06:26 AM
PlausiblyDamp's Avatar
PlausiblyDampMemory mgmt issue PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You could make sure the DataAdapter is disposed of by wrapping it in a Using block and you probably don't need to clear the dstBackupScan DataSet anyway - it will become the garbage collector's problem anyway and the memory will be reclaimed automatically. Those changes and a couple of other tweaks would give you something like
Code:
Using adaBackupScan = New SqlDataAdapter(ConnString("BackupsSQL"), "Select TOP 1 * From Backups where Req = 1 Order by BackupTime ASC")
    Try
        Dim dstBackupScan As New DataSet
        adaBackupScan.Fill(dstBackupScan, "AutoBackups")
        If dstBackupScan.Tables("AutoBackups").Rows.Count > 0 Then
            BackUpFiles()
        End If
    Catch ex As Exception
        Call WriteToLog(ex.ToString)
    End Try

    Try
        System.GC.Collect()
    Catch ex As Exception
         Call WriteToLog(ex.ToString)
     End Try
End Using
That would result in the Dispose always being called (to all practical purposes anyway).
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Memory mgmt issue
Memory mgmt issue
Memory mgmt issue Memory mgmt issue
Memory mgmt issue
Memory mgmt issue
Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue Memory mgmt issue
Memory mgmt issue
Memory mgmt issue
 
Memory mgmt issue
Memory mgmt issue
 
-->