Click here to Skip to main content
14,971,280 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What you have told previously is reminded by you and that's true plus one more problem of using static variable is that after it is called it remains active in memory or to say it just occupies space in memory as long as the program is running....
It's also true that I should have a very good reason to use static.
Sir if I explicitly give a null value to it then I think the space taken by the variable is freed ... And the program will have no performance I right sir.. ?

What I have tried:

I I have tried to just increase my knowledge over static.
Updated 9-Feb-21 4:24am
PIEBALDconsult 9-Feb-21 10:10am
I think you need to study references more deeply. A reference to an object is not the object itself.
An object in memory doesn't know whether it has references which are static or not.
Once all references to the object have been removed (such as setting to null) it can be garbage collected -- whether or not those references were static is immaterial.
Member 12712527 9-Feb-21 10:20am
This static can only be garbage collected once it is freed but if it remains active all the time until the program ends I hope it isn't garbage collected so there is a performance problem. . maybe I think
PIEBALDconsult 9-Feb-21 10:34am
No, do not think of the object itself as static, it isn't, it's just an object, it can have many references, some may be static and others not.
Member 12712527 9-Feb-21 10:40am
An object in memory maybe static may not be but my question is that as only in the case of static I am thinking that as it remains active all the time in memory until the program ends it is not garbage collected which may lead to performance problem is it right sir
PIEBALDconsult 9-Feb-21 10:53am
No, do not think of the object itself as static, it isn't, it's just an object, it can have many references, some may be static and others not.
Member 12712527 9-Feb-21 10:58am
Same answer as above
Member 12712527 9-Feb-21 11:04am
Though space is no problem for a normal PC but it might be the headache to programmer regarding a telephone exchange company or a Web server....
PIEBALDconsult 9-Feb-21 11:25am
I understand your concern, but you do not seem to understand references.
Member 12712527 9-Feb-21 11:38am
Any type of variable it is I want to state that if it remains active all the time during program run it's not garbage collected so space is acquired all the time. .if the program never ends then that space is acquired permanently it's a problem. If it's an instance variable then it's value automatically goes away once it is out of scope so no problem regarding instance variable but problem is there regarding static
Member 12712527 9-Feb-21 11:42am
So setting it to null frees itself and I think there remains no problem
PIEBALDconsult 9-Feb-21 12:16pm
Quite likely, unless there are other references as well.
Member 12712527 9-Feb-21 11:47am
It's not about references but the question is about performance so sir awaiting for your answer eagerly
PIEBALDconsult 9-Feb-21 12:27pm
Contrariwise, it's entirely about references and their effect on object lifetimes as pertains to performance.
Member 12712527 9-Feb-21 12:06pm
Okay sir its night okay good night sir

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900