8

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 ?

Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
  • Some similar questions: http://stackoverflow.com/questions/16456366/why-does-the-size-of-a-class-depends-on-the-order-of-the-member-declaration-and http://stackoverflow.com/questions/2006504/c-data-alignment-member-order-inheritance – Piotr Siupa Jul 21 '15 at 08:07
  • 1
    The JVM specification states that [*Successive items are stored in the class file sequentially, without padding or alignment*](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html). – Turing85 Jul 21 '15 at 08:10
  • 1
    @NO_NAME I don't know what similar means for you, but that is not what similar means to me. Did you read my question ? – Othman Benchekroun Jul 21 '15 at 08:14
  • @Turing85 The sentence before : "the contents of the structures describing the class file format are referred to as items", I think they are not talking about the class fields – Othman Benchekroun Jul 21 '15 at 08:19
  • @OthmanBenchekroun I'm not saying that these questions will solve your problem. However, they can be helpful for someone. – Piotr Siupa Jul 21 '15 at 08:19
  • @OthmanBenchekroun point taken. Other than that, I was not able to find the mentioning of *padding* in relevant parts of the JLS or VM Specification. – Turing85 Jul 21 '15 at 08:22
  • @NO_NAME alright, I see what similar means to you – Othman Benchekroun Jul 21 '15 at 08:23
  • I don't have an answer to your question, but I don't think the compiler can choose how objects will be stored in memory : the JVM will. And the way objects are stored is highly dependent on the garbage collector you're using. – yannick1976 Jul 21 '15 at 19:29
  • @yannick1976 Do you have anything confirming this ? – Othman Benchekroun Jul 21 '15 at 19:52
  • All that a Java compiler does is producing .class files, and data in a .class file is very much the same as in a .java file (that's one of the reasons java decompilers work so good). I don't see anything there about how objects are store in memory. More info in [the official specification](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html) – yannick1976 Jul 21 '15 at 21:51

1 Answers1

1

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
Bertram Nudelbach
  • 1,783
  • 2
  • 15
  • 27