Do Static Members of a Class Occupy Memory If No Object of That Class Is Created

Do static members of a class occupy memory if no object of that class is created?

No.

Static members don't belong to the instances of class. They don't increase instances and class size even by 1 bit!

struct A
{
int i;
static int j;
};
struct B
{
int i;
};
std::cout << (sizeof(A) == sizeof(B)) << std::endl;

Output:

1

That is, size of A and B is exactly the same. Static members are more like global objects accessed through A::j.

See demonstration at ideone : http://www.ideone.com/YeYxe


$9.4.2/1 from the C++ Standard (2003),

A static data member is not part of
the subobjects of a class. There is
only one copy of a static data member
shared by all the objects of the
class.

$9.4.2/3 and 7 from the Standard,

once the static data member has been
defined, it exists even if no objects
of its class have been created.

Static data members are initialized
and destroyed exactly like non-local
objects (3.6.2, 3.6.3)
.

As I said, static members are more like global objects!

Do Static Methods and Fields take up memory in an instance of the class they are defined in?

No, static methods and fields do not take space in an instance of the class.

You are making a number of confusions here. When you compile your program, the code of each of your method (static or not) is "stored" in your compiled program (in the case of Java, in .class files). When you execute a program, static members - that "belong" to a class, like field1 in your question - are allocated once for your whole program. The other "normal" class members - like field2 in your question - are allocated for each new instance you create.

When you create a new instance of an object, the code of its various methods is not "allocated", as it already exists in compiled form in your program.

Memory Allocation of Static Members in a Class

Obviously, it takes memory. And int A::obj_s=0 is exactly what it does: it defines the variable along with it's memory. In fact, when we say we defined a variable X, that means we define a memory of sizeof(X), and that memory region we label as X.


More about static members:

A::obj_s is a static member of the class A. And static members exist without any instance. They're not part of instances of A.

§9.4.2/3 and 7 from the Standard,

once the static data member has been
defined, it exists even if no objects
of its class have been created.

Static data members are initialized
and destroyed exactly like non-local
objects (3.6.2, 3.6.3).

Read my complete answer here:

Do static members of a class occupy memory if no object of that class is created?

Why are the static members of a class the same for all objects?

Because they would be instance members then.

The primary characteristic of static members is that they're shared by all the instances of the class.

Do static members help memory efficiency?

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.

static class member of class's own type

The reason for class can't have data members of its own type is the compiler must know the size of class object.
For example, one class is a local variable in function, the compiler can handle the stack only it knows the class size.

For your case, the static class member doesn't reside in class object, so has no impact to size of class object. It's OK.

What is the actual memory place for static variables?

Static fields are initialised when a class is loaded and are discarded when the classloader for that class is unloaded. They can be cleaned up, even duplicated in another class loader.

For applications like those that use OSGi, static variables don't live for the whole life of the application. They can be reloaded many times.

How this is implement may be JVM dependent, but the Sun/Oracle JVM creates an "object" to hold the static fields for a class. This object is accessible via the Unsafe class which can also be used to examine this "objects" fields.

How can class definitions not occupy memory?

It is not entirely correct to say that class definitions do not occupy memory: any class with member functions may place some code in memory, although the amount of code and its actual placement depends heavily on function inlining.

The Q&A at the first link talks about sizeof, which shows a per-instance memory requirement of the class, which excludes memory requirements for storing member functions, static members, inlined functions, dispatch tables, and so on. This is because all these elements are shared among all instances of the class.



Related Topics



Leave a reply



Submit