Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do static members help memory efficiency?

If I have a class that I expect to be used in thousands of instances in a memory-sensitive application, does it help if I factor out static functionality to static members?

I imagine that static methods and variables are stored once per class while for non-static members there has to be something stored for each instance.

With member variables, it seems quite clear, but what kind of data is stored for methods?

I'm working in Java, but I imagine some general rules to apply in other managed environments (such as .NET), too.

like image 792
Hanno Fietz Avatar asked Sep 30 '09 07:09

Hanno Fietz


People also ask

Do static variables take more memory?

A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once.

Why static is used in memory management?

This keyword is mainly used for the management of memory. We can use static keyword with methods, variables, class, and blocks. It belongs to a class rather than the instance of the class, It simply means that if you make any variable static to can access it without its object.

Can static methods cause memory leaks?

So, if you are using 100 static methods in your program, when the program starts all methods are loaded into memory and will fill the memory unnecessarily. Furthermore static methods increase the risk of memory leaks.

What is the advantage of using static?

Advantage #1: You can detects bugs and errors early Static type checking allows us to verify that the invariants we specified are true without actually running the program. And if there's any violation of those invariants, they will be discovered before runtime instead of during it.


2 Answers

The only difference between static methods and non-static (instance) methods behind the scenes is that an extra, hidden parameter (this) is passed to instance methods and that instance methods might be called using an indirect dispatch (if virtual). There is no additional code space taken.

Edit:


My answer focused on methods, but on closer reading I see that the question is more about static data. Yes, static data will in a sense save memory since there's only a single copy of it. Of course, whether or not data should be static is more a function of the meaning or use of the data, not memory savings.

If you need to have a large number of objects and want to conserve memory, you may want to also investigate if using the 'Flyweight' pattern is applicable.

like image 154
Michael Burr Avatar answered Oct 23 '22 12:10

Michael Burr


The decision shouldn't be made on the grounds of efficiency - it should be made on the grounds of correctness.

If your variable represents a distinct value for each instance, it should be an instance variable.

If your variable is a common value associated with the type rather than an individual instance of the type, it should be a static variable.

You're correct, however - if you have a static variable, you won't "pay" for that with every instance. That just adds an extra reason to make variables static where they don't represent part of the state of the object.

When you mention methods in your question, are you talking about local variables? You'll get a new set of local variables for every method call - including recursive calls. However, this doesn't create a new set of static or instance variables.

like image 44
Jon Skeet Avatar answered Oct 23 '22 12:10

Jon Skeet