Hello, I have written a windows service in vb.net but I have a memory leak and I believe the issue is because I am not sure how to declare and dispose of variables and objects. The service runs on a timer which elapses every 2 seconds. It takes about 10 to 30 seconds to complete each run thru. I currently have all of my code on a single page but it is split up into many sub routines and functions. The service runs constantly and is never closed.
My question is basically how should I declare my variables and objects.
For instance, if I use a data reader in a sub should I declare it within the sub or globally at the top of the page. Currently, I declare the data reader in the beginning of the sub but I do not dispose of it. Should I dispose it at the end of the sub? If I dispose it will it be able to be declared again when the timer elapses again? Also, since the timer elapses every 2 seconds and its possible for the processes to overlap, what happens if the data reader is declared by one process but a process which started later disposes of that data reader before the original process does. Will the original process still be able to access that data reader?
I hope I explained well enough. If not let me know a. Thanks!!
Start Free Trial