Static properties and memory release

Asked

Viewed 478 times

2

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

My project is unit test 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 there are no new instances of the browser for each method executed. It is a practice not recommended?

I happen to have several classes for testing. All inheriting base. I read in various places that static objects, methods and classes are not released from memory in the normal GC process.

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

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

Or, 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();
}

This will lead to memory release after execution?

How do I track all the objects loaded in memory at runtime and their respective releases? Is there a way?

  • Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

1 answer

2


The object in the static variable won’t be released because the memory is static, it wouldn’t 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 other references to it. If you have no reason to release too.

Then you need to ask yourself whether the object should continue to exist or not. If it should not, just cancel it (null) in the static variable.

That method Finalizar() looks like a beautiful gambiarra. Can you guarantee that in all situations this method will be called? It is necessary?

The close() should probably be inside the dispose(). That one dispose() is it necessary? He does what? What I saw in the code There seems to be no such method. If it did exist, it probably would have to be called somewhere, but it doesn’t seem to have a place to do it. And it probably should using.

The Iniciar() also doesn’t seem to make any sense.

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

This means that for each first test method run in each class, I will have a load in the memory of the property driverGC 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 previous one will be discarded as soon as possible if you have no new references to it. Nothing needs to be done.

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

What matters is that the property is static. The only thing a static class does is not allow nonstatic members to exist.

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

I don’t understand what this is, but with all the explanation I think we can conclude what you want to know.

This will lead to memory release after execution?

No, read further below. And note what I have already written above.

Ah, and how do I keep track of all the objects loaded in memory at runtime and their respective releases? Is there a way?

Something could be on its own Debugger Visual Studio or other IDE. Others with only one profiler.

Complementary readings:

  • The Start function in context is to add the modifiers and parameter in the execution of the test methods. Right? Otherwise I would have to add to each method the parameter and the static modifier, since they are required. It is okay to use this methodology, right? The case of Finalizar, yes, always passes this method when it has completed the tests of this class. Remembering that I implemented both in all classes. Since all my classes have the property of Iwebdriver static and it should not be inherited, then I should explicitly put in the class, this?

  • What I would like to avoid is to have a lot of objects in memory that I no longer need. I did the test by removing the static modifier from the property and it opens for each test a new instance of the browser... So if in the method finish I assign null the property, what changes, if it will be there until the end of the form qlqr application? Have patience if it’s boring but it’s hard to understand and accept..

  • I don’t know, there’s something specific about framework test that requires it? That context is used somewhere? If it is to be in all classes it should be in Base, but still worth the rest I said, it doesn’t make sense in something static, and even less to use it in something that doesn’t have it. The impression I get is that you’re trying to do a lot of things without knowing why, this doesn’t work. It seems to me that it should be static even and not inherit it. In fact it seems to have so many errors in this code that it is even difficult to understand what you really want. I don’t understand what use most of what’s in it.

  • Yes, it is required that all test methods are static and have this object as their parameter. But this is not the case. And thank you for the static property, I’m transferring you to the classes. As for the effective doubt, not belittling the other information you gave me, if I pass null for this purpose, will I have any gain? Or is it the same as not doing it?

  • In fact I think that it is not the case to move to classes is that it makes no sense to have this inheritance. If you want to have a single instance of the browser should be static in a separate class that should not be inherited, and not have multiple copies of it. I don’t know exactly the context, but it seems to me that the null won’t be useful if you do it right. Problem is I don’t know if you’re right.

  • I changed the code in the post according to your notes. Yes, actually it should be like you said. The driver should be passed in another form to the classes, as thus, each test class executes a browser instance. So, no null and I will treat this object as a static object according to its purpose. Thanks for the help, @bigown

  • I shouldn’t be doing this because you misread the question, so my answer now is about something that is no longer in the question. I still don’t know if this is a good solution, because everything is still strange.

  • I returned the edition. So what’s strange? I came looking for help, feel free.

  • I don’t know, the whole code makes little sense to me, I don’t really know what I should do.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.