A simple code with huge mem usage!!!!

all41

New member
Joined
Feb 17, 2005
Messages
2
Programming Experience
Beginner
I have a Windows service that runs perfect. It checks a print-Q and deletes unwanted jobs. The problem I'm having is when I monitor the Mem Usage in Windows Task Manager I notice that the amount of memory used by the web service keeps increasing to a dangerous piont. I'm trying to find what causes the memory to increase.

My windows service uses a time object with interval=4000. and below is the only code I would like to run.

PrivateSub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

Dim kNameSpace AsString = "root\cimv2"
Dim DocName AsString = "HL7"
Dim Jobs
Dim oJob
Dim oService
Dim oLocator
oService = Nothing
oLocator = CreateObject("WbemScripting.SWbemLocator")
oService = oLocator.ConnectServer(, kNameSpace, , )
oService.Security_.impersonationlevel = 3

' Required to perform administrative tasks on the spooler service
oService.Security_.Privileges.AddAsString("SeLoadDriverPrivilege")
Jobs = oService.InstancesOf("Win32_PrintJob")
ForEach oJob In Jobs
If Mid(oJob.Document, 1, 3) = DocName Then
Try
oJob.Delete_()
Catch ex As Exception
EndTry
EndIf
Next

oService.Dispose()
oLocator.Dispose()
Jobs.Dispose()
oJob.Dispose()
DocName =
Nothing
kNameSpace = Nothing
e = Nothing
sender.dispose()
EndSub

 
The problem may be that you're creating and disposing all the objects (oService, oLocator, Jobs, and oJobs) many times. The garbage disposal will eventually remove the unused memory, but it can take some time.
I would suggest creating those objects at a class level; instantiate them when the service starts, and dispose of them when the service ends. Then include only the 'For Each ... Next' code in the timer's elapsed event handler.
 
I was having the problem before adding these lines. as a matter of fact, I added these lines because of that. I'll try your second suggestion and declare my objects in the class level. Thank you for the respond.
 
Back
Top