I am checking the memory usage of my service and found out that the private bytes are increasing with time. The service creates the new components it requires and they only get disposed when the service stops. So I am searching for the code which creates new components when there is a service call and trying to dispose them when they are not required. I am using PerfMon to check the private bytes.
So am I on the right track till now?
Moreover, I have found that one of the method creates new struct every time and then its not gets disposed or assigned to null.
var structInfo = new MyStruct();
structInfo .StructSize = Marshal.SizeOf(structInfo);
MyClass info = this.BuildStructInfo(structInfo);
return info;
EDIT:
The method returns the class which gets its values from the struct.
Do I need to dispose the structInfo, will it be GC collected if I don’t do anything?
A struct is a value type (like an int for example). It will be automatically be GC’ed if it goes out of scope.
You could sometimes see the memory usage growing and growing, without any memory leak at all: if there is plenty of memory available, your GC won’t run that often.
I suggest to use a memory profiler, which can show you why some objects are kept alive. I can recommend YourKit for .NET profiler, which has a 30-day free trial: