Static properties and memory release

I am having some doubts regarding releasing the features for cases where my modifiers are static.

My Project is unit test and I am using Selenium for the first time.

public class LoginTest : Base
{
        [ClassInitialize]
        public static void Iniciar(TestContext context) { }

        [TestMethod]
        public void Logar()
        {
            var loginPage = new LoginPage(driverGC);
            loginPage.Navegar("http://localhost:3533/Authorize/LogOn").Logar("[email protected]", "123456");
            Assert.IsTrue(loginPage.VerificarMenuLateral());
        }

        ...
}

public abstract class Base
{
        protected static IWebDriver driverGC = new ChromeDriver(@"C:\chromedriver_win32");
}

The property is static so that all tests occur with reference to what has already been obtained and so that there are no new instances of the browser with each method executed. Is it a non-recommended practice?

It turns out that I have several classes for test. All inheriting from base. I have read in several places that static objects, methods and classes are not released from memory in the normal GC process.

Does this mean that for every first test method run in each class, will I have a load in the memory of the driverGC property that will never be released?

Or, being the property of a class that is not static, will it be removed along with the Class release?

Or else this property will come to the next class with the values obtained in the execution of the previous class?

I implemented within each test class a method:

[ClassCleanup]
public static void Finalizar()
{
    driverGC.Close();
    driverGC.Dispose();
}

Will this result in memory release after execution?

Should I keep track of all objects loaded in memory at runtime and their respective releases? Have how?

Author: Maniero, 2017-05-19

1 answers

The object in the static variable will not be released because the memory is static, it would make no sense to release that. But the object that is referenced in the static variable can be released yes, as long as there are no other references to it. If you have no need to release too.

Then you need to wonder if the object should continue to exist or not. If it should not, just override it (null) in the static variable.

This method Finalizar() looks like a beautiful gambiarra. You can guarantee that in all situations this method will be called? Is it necessary?

The close() should probably be inside the dispose(). Is this dispose() necessary? He does what? What I saw in the code it seems that there is no such method. If it existed, it would probably have to be called somewhere, but it doesn't seem like it has a place to do that. And you should probably use using.

The Iniciar() doesn't seem to make any sense either.

Static members not are inherited, so this class Base doesn't even seem to make sense.

Does this mean that for every first test method run in each class, will I have a load in the memory of the driverGC property that will never be released?

Not only will there be one in every application. If its value is not explicitly changed there is only one object referenced by it. Even if you change to a new object, the previous one will be discarded as soon as possible if it does not have new ones references to him. Nothing needs to be done.

Or, being the property of a class that is not static, will it be removed along with the Class release?

What Matters is the property being static. The only thing a static class does is not allow non-static members to exist, plus of course not allow inheriting, which wouldn't make sense.

Or, still, this property will come to the next class with the values obtained in the execution from the previous class?

I did not understand what this is, but with all the explanation already given I think you can conclude what you want to know.

Will this result in memory release after execution?

No, read more below. And note what I already wrote above.

Oh, and how do I keep track of all objects loaded into memory at runtime and their respective releases? Have how?

Something can be by own debugger from Visual Studio or another IDE. Other only with one profiler.

Additional reading:

 2
Author: Maniero, 2020-05-19 15:15:37