Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are class attributes/fields stored

I know that an instance of this C++ class :

class A {
   char c;
   int iiii;
   short ss;   
};

would look kind of like this in memory : c| | | |i|i|i|i|s|s| | |

This 4/2 letter annotation has no sense (But I think that my point is clear)

1 byte for char, 3 bytes of padding, 4 bytes of int, 2 bytes for the short, and 2 bytes of tail padding (platform dependant, but it won't change the logic)

From C++ standards (Compilers wouldn't change the order of the fields in my example):

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (Clause 11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

so, I would like to know if it is the same for Java classes, Can the compiler change the order to reduce the padding ?

like image 304
Othman Benchekroun Avatar asked Jul 21 '15 07:07

Othman Benchekroun


People also ask

What is a class attribute stored as in Java?

Java Classes can have some fields and methods that represent the individual properties and behavior/actions of the class. The fields also known as class attributes are nothing but the variables declared within the class. For instance, the Student is a class then the student's roll no, name, section, etc.

Are classes stored on stack or heap?

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.

How are attributes added to a class?

Adding attributes to a Python class is very straight forward, you just use the '. ' operator after an instance of the class with whatever arbitrary name you want the attribute to be called, followed by its value.

Where are class attributes defined?

Class attributes are attributes which are owned by the class itself. They will be shared by all the instances of the class. Therefore they have the same value for every instance. We define class attributes outside all the methods, usually they are placed at the top, right below the class header.


1 Answers

First have a look at What do Java objects look like in memory during run-time? and Know Thy Java Object Memory Layout .

But I guess Java Objects Memory Structure answers your question.

In order to save some memory, the Sun VM doesn't lay out object's attributes in the same order they are declared. Instead, the attributes are organized in memory in the following order:

doubles and longs
ints and floats
shorts and chars
booleans and bytes
references
like image 62
Bertram Nudelbach Avatar answered Sep 18 '22 10:09

Bertram Nudelbach