Hi,
Its not a case of it "consuming more memory" per se, its more about when the memory is freed up..
I would
strongly recommend NEVER putting a DataSet at static class level.
If its at [instance] class level, the DataSet will not be destroyed until the instance of the class is..
If its at procedural level, then it is destroyed when it is out of scope within the procedure, for example:
Code:
void SomeMethod()
{
DataSet ds1 = GetDataSet1(); // Code omitted for clarity
if (something)
{
DataSet ds2 = GetDataSet2(); // Code omitted for clarity
// Some more code
} // ds2 will be tagged for GC here
else
// Something else
}
// Other method code
} // ds1 will be tagged for GC here
Note: I didn't say "destroyed". I always keep in the back of my mind that you cannot guarantee when objects will be destroyed due to the garbage collection model of .NET.
As for "best practice". Always scope things as low as possible. If you only need it within a method, only use it in a method. If you need it higher, then you may need to think about the data access model more. For example. A customer database with 10M records is used across the application, but you wouldnt throw that in a DataSet at application scope, would you? ;)
I hope this helps.
Rob
http://cantgrokwontgrok.blogspot.com