Static properties and memory release

2

I have some doubts regarding the release of resources for cases where my modifiers are static.

My project is unit testing and I'm 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 to not have new instances of the browser with each method executed. Is it a practice not recommended?

It turns out that I have several classes to test. All inheriting 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 executed in each class, will I have a memory load of the driverGC property that will never be released?

Or, being the property of a class that is not static, will it be removed next to the class release?

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

Implemented within each test class a method:

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

Will this lead to memory leak after execution?

How do I track all objects loaded in memory at runtime and their respective releases? How?

    
asked by anonymous 19.05.2017 / 16:46

1 answer

2

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

So you have to ask yourself whether the object should continue to exist or not. If not, simply cancel it ( null ) in the static variable.

This Finalizar() method looks like a beautiful game. Can you guarantee that in all situations this method will be called? Is it necessary?

The close() should probably be within dispose() . Is this dispose() required? He does what? What I've seen in code does not seem to exist this method . If it existed, it would probably have to be called somewhere, but it does not look like it has a place to do that. And you should probably use using .

The Iniciar() also does not seem to make any sense.

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

  

Does this mean that for every first test method executed in each class, will I have a load on 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 will only be one object referenced by it. Even if you switch to a new object, the old one will be discarded as soon as possible if you do not have any new references to it. Nothing needs to be done.

  

Or, being the property of a class that is not static, will it be removed next to the class release?

What matters is whether the property is static. The only thing a static class does is not allow non-static members to exist, besides of course not allowing inheritance, which would not make sense.

  

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

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

  

Will this lead to memory leak after execution?

No, read below. And notice what I wrote above.

  

Oh, and how do I track all objects loaded in memory at runtime and their respective releases? How?

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

Further reading:

19.05.2017 / 17:28