0
votes

I'm using VB6 with ADODB connection to connect to sql server 2012 database and execute queries on the database, I'm facing memory leak where I see that memory usage does not decrease after query finished even after I terminate my program the memory usage dose not decrease.

Below is my code:

Public adoCon As New ADODB.Connection

Public serverName As String, databaseName As String, tableName As String Public sqlCmd As String

Public Sub adodb_Connect()

If Tenzfrm.adoCon.State = adStateOpen Then Tenzfrm.adoCon.Close
serverName = Tenzfrm.server_Name.Text
databaseName = Tenzfrm.database_Name.Text
Tenzfrm.adoCon.Provider = "sqloledb"
Tenzfrm.adoCon.Properties("Data Source").Value = serverName
Tenzfrm.adoCon.Properties("Initial Catalog").Value = databaseName
Tenzfrm.adoCon.Properties("Integrated Security").Value = "SSPI"
Tenzfrm.adoCon.CommandTimeout = 0
Tenzfrm.adoCon.Open

End Sub

Public Sub adodb_Close()

If Tenzfrm.adoCon.State = adStateOpen Then Tenzfrm.adoCon.Close
Set Tenzfrm.adoCon = Nothing

End Sub

Private Sub appHourlySTS_Click()

Call adodb_Connect
proccess.Caption = "Preparing tables !"
proBar2.Width = 2 * proBar1.Width / 100
sqlCmd = "IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Tenz_Cell_STS_Hourly_Temp') DROP TABLE Tenz_Cell_STS_Hourly_Temp;"
adoCon.Execute (sqlCmd)
sqlCmd = "DECLARE @sqlCmd NVARCHAR(MAX) = 'CREATE TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp](';"
sqlCmd = sqlCmd & "SELECT @sqlCmd+='['+Headers+'] FLOAT,' FROM [dbo].[Tenz_colReg];"
sqlCmd = sqlCmd & "SET @sqlCmd=LEFT(@sqlCmd,LEN(@sqlCmd)-1)+')';"
sqlCmd = sqlCmd & "EXEC(@sqlCmd);"
adoCon.Execute (sqlCmd)
sqlCmd = "ALTER TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp] ALTER COLUMN [Date] VARCHAR(23);"
sqlCmd = sqlCmd & "ALTER TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp] ALTER COLUMN [Time] VARCHAR(10);"
sqlCmd = sqlCmd & "ALTER TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp] ALTER COLUMN [NE Name] VARCHAR(15);"
sqlCmd = sqlCmd & "ALTER TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp] ALTER COLUMN [Cell Name] VARCHAR(64);"
sqlCmd = sqlCmd & "ALTER TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp] ALTER COLUMN [CI] VARCHAR(64);"
sqlCmd = sqlCmd & "ALTER TABLE [dbo].[Tenz_Cell_STS_Hourly_Temp] ALTER COLUMN [LAC] VARCHAR(10);"
adoCon.Execute (sqlCmd)
proccess.Caption = "Appending statistics to 'Tenz_STS_Cell_Hourly' table from CS1 !"
proBar2.Width = 5 * proBar1.Width / 100
sqlCmd = "DECLARE @sqlCmd NVARCHAR(MAX) = 'INSERT INTO [dbo].[Tenz_Cell_STS_Hourly_Temp] '; "
sqlCmd = sqlCmd & "DECLARE @sc NVARCHAR(MAX) = '';"
sqlCmd = sqlCmd & "SELECT @sc+='['+Name+']=[dbo].[Tenz_CS1].['+Name+'],' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_CS1') AND ([Name]='Date' OR [Name]='Time' OR [Name]='Cell Name' OR [Name]='NE Name' OR [Name]='CI' OR [Name]='LAC');"
sqlCmd = sqlCmd & "SELECT @sc+='['+Name+']=CASE WHEN TRY_CONVERT(FLOAT,[dbo].[Tenz_CS1].['+Name+']) IS NULL THEN ''0'' ELSE [dbo].[Tenz_CS1].['+Name+'] END,' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_CS1') AND [Name]<>'Date' AND [Name]<>'Time' AND [Name]<>'Cell Name' AND [Name]<>'NE Name' AND [Name]<>'CI' AND [Name]<>'LAC';"
sqlCmd = sqlCmd & "SET @sc=LEFT(@sc,LEN(@sc)-1);"
sqlCmd = sqlCmd & "DECLARE @dc NVARCHAR(MAX) = ''; "
sqlCmd = sqlCmd & "SELECT @dc+='['+Name+'],' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_CS1') AND ([Name]='Date' OR [Name]='Time' OR [Name]='Cell Name' OR [Name]='NE Name' OR [Name]='CI' OR [Name]='LAC');"
sqlCmd = sqlCmd & "SELECT @dc+='['+Name+'],' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_CS1') AND [Name]<>'Date' AND [Name]<>'Time' AND [Name]<>'Cell Name' AND [Name]<>'NE Name' AND [Name]<>'CI' AND [Name]<>'LAC';"
sqlCmd = sqlCmd & "SET @dc=LEFT(@dc,LEN(@dc)-1);"
sqlCmd = sqlCmd & "SET @sqlCmd+='('+@dc+') SELECT '+@sc+' FROM [dbo].[Tenz_CS1]';"
sqlCmd = sqlCmd & "EXEC sp_executesql @sqlCmd;"
adoCon.Execute (sqlCmd)
proccess.Caption = "Appending statistics to 'Tenz_STS_Cell_Hourly' from CS2 !"
proBar2.Width = 35 * proBar1.Width / 100
sqlCmd = "DECLARE @sqlCmd NVARCHAR(MAX) = 'MERGE INTO [dbo].[Tenz_Cell_STS_Hourly_Temp] USING [dbo].[Tenz_CS2] ON [dbo].[Tenz_Cell_STS_Hourly_Temp].Date = [dbo].[Tenz_CS2].Date AND [dbo].[Tenz_Cell_STS_Hourly_Temp].Time = [dbo].[Tenz_CS2].Time AND [dbo].[Tenz_Cell_STS_Hourly_Temp].[Cell Name] = [dbo].[Tenz_CS2].[Cell Name] WHEN MATCHED THEN UPDATE SET ';"
sqlCmd = sqlCmd & "DECLARE @sc NVARCHAR(MAX) = '';"
sqlCmd = sqlCmd & "SELECT @sc+='['+Name+']=CASE WHEN TRY_CONVERT(FLOAT,[dbo].[Tenz_CS2].['+Name+']) IS NULL THEN ''0'' ELSE [dbo].[Tenz_CS2].['+Name+'] END,' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_CS2') AND [Name]<>'Date' AND [Name]<>'Time' AND [Name]<>'Cell Name' AND [Name]<>'NE Name' AND [Name]<>'CI' AND [Name]<>'LAC';"
sqlCmd = sqlCmd & "SET @sc=LEFT(@sc,LEN(@sc)-1)+';';"
sqlCmd = sqlCmd & "SET @sqlCmd+=@sc;"
sqlCmd = sqlCmd & "EXEC sp_executesql @sqlCmd;"
adoCon.Execute (sqlCmd)
proccess.Caption = "Appending statistics to 'Tenz_STS_Cell_Hourly' from PS1 !"
proBar2.Width = 50 * proBar1.Width / 100
sqlCmd = "DECLARE @sqlCmd NVARCHAR(MAX) = 'MERGE INTO [dbo].[Tenz_Cell_STS_Hourly_Temp] USING [dbo].[Tenz_PS1] ON [dbo].[Tenz_Cell_STS_Hourly_Temp].Date = [dbo].[Tenz_PS1].Date AND [dbo].[Tenz_Cell_STS_Hourly_Temp].Time = [dbo].[Tenz_PS1].Time AND [dbo].[Tenz_Cell_STS_Hourly_Temp].[Cell Name] = [dbo].[Tenz_PS1].[Cell Name] WHEN MATCHED THEN UPDATE SET ';"
sqlCmd = sqlCmd & "DECLARE @sc NVARCHAR(MAX) = '';"
sqlCmd = sqlCmd & "SELECT @sc+='['+Name+']=CASE WHEN TRY_CONVERT(FLOAT,[dbo].[Tenz_PS1].['+Name+']) IS NULL THEN ''0'' ELSE [dbo].[Tenz_PS1].['+Name+'] END,' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_PS1') AND [Name]<>'Date' AND [Name]<>'Time' AND [Name]<>'Cell Name' AND [Name]<>'NE Name' AND [Name]<>'CI' AND [Name]<>'LAC';"
sqlCmd = sqlCmd & "SET @sc=LEFT(@sc,LEN(@sc)-1)+';';"
sqlCmd = sqlCmd & "SET @sqlCmd+=@sc;"
sqlCmd = sqlCmd & "EXEC sp_executesql @sqlCmd;"
adoCon.Execute (sqlCmd)
proccess.Caption = "Appending statistics to 'Tenz_STS_Cell_Hourly' from PS2 !"
proBar2.Width = 65 * proBar1.Width / 100
sqlCmd = "DECLARE @sqlCmd NVARCHAR(MAX) = 'MERGE INTO [dbo].[Tenz_Cell_STS_Hourly_Temp] USING [dbo].[Tenz_PS2] ON [dbo].[Tenz_Cell_STS_Hourly_Temp].Date = [dbo].[Tenz_PS2].Date AND [dbo].[Tenz_Cell_STS_Hourly_Temp].Time = [dbo].[Tenz_PS2].Time AND [dbo].[Tenz_Cell_STS_Hourly_Temp].[Cell Name] = [dbo].[Tenz_PS2].[Cell Name] WHEN MATCHED THEN UPDATE SET ';"
sqlCmd = sqlCmd & "DECLARE @sc NVARCHAR(MAX) = '';"
sqlCmd = sqlCmd & "SELECT @sc+='['+Name+']=CASE WHEN TRY_CONVERT(FLOAT,[dbo].[Tenz_PS2].['+Name+']) IS NULL THEN ''0'' ELSE [dbo].[Tenz_PS2].['+Name+'] END,' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_PS2') AND [Name]<>'Date' AND [Name]<>'Time' AND [Name]<>'Cell Name' AND [Name]<>'NE Name' AND [Name]<>'CI' AND [Name]<>'LAC';"
sqlCmd = sqlCmd & "SET @sc=LEFT(@sc,LEN(@sc)-1)+';';"
sqlCmd = sqlCmd & "SET @sqlCmd+=@sc;"
sqlCmd = sqlCmd & "EXEC sp_executesql @sqlCmd;"
adoCon.Execute (sqlCmd)
proccess.Caption = "Finishing !"
proBar2.Width = 80 * proBar1.Width / 100
sqlCmd = "DECLARE @sqlCmd NVARCHAR(MAX) = 'INSERT INTO [dbo].[Tenz_Cell_STS_Hourly] '"
sqlCmd = sqlCmd & "DECLARE @sc NVARCHAR(MAX) = ''; "
sqlCmd = sqlCmd & "SELECT @sc+='['+Name+'],' FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE type = 'U' AND [Name] = 'Tenz_Cell_STS_Hourly');"
sqlCmd = sqlCmd & "SET @sc=LEFT(@sc,LEN(@sc)-1);"
sqlCmd = sqlCmd & "SET @sqlCmd+='('+@sc+') SELECT '+@sc+'FROM [dbo].[Tenz_Cell_STS_Hourly_Temp]';"
sqlCmd = sqlCmd & "EXEC sp_executesql @sqlCmd;"
adoCon.Execute (sqlCmd)
proBar2.Width = 100 * proBar1.Width / 100
MsgBox ("Statistics appended to 'Tenz_STS_Cell_Hourly' successfully !")
proBar2.Width = 0
proccess.Caption = "Idle ! "
sqlCmd = "IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Tenz_Cell_STS_Hourly_Temp') DROP TABLE Tenz_Cell_STS_Hourly_Temp;"
adoCon.Execute (sqlCmd)
Call adodb_Close

End Sub

1
Can you just output the generated SQLCMD text, so it is easier to see the SQL generated?Dominic Zukiewicz
Just to be clear, are you talking about memory usage on a client, or on the SQL Server host?Dave Mason
Hi Dominic Zukiewicz, the columns in my tables is about 200 columns, so it's hard to put the generated SQLCMD also the column names is too long.Anas Alwindawee
Hello DMason, Actually I did not get your question, but I'm talking about the memory usage of my PC where sql server is installed. hope that's helps.Anas Alwindawee

1 Answers

1
votes

I just had the same problem. SQL server will always use as much memory as you allow it to. You have to set a limit.

SSMS not releasing system memory https://dba.stackexchange.com/questions/47431/why-is-sql-server-consuming-more-server-memory

Right click on the server in SSMS and select properties. In the memory tab set the Maximum server memory option to limit SQLs memory usage.

enter image description here