Is memory needed for short fields in Dalwick?

Java virtual machines can use int dimensional widths for short fields (this depends on their internal implementation). The only exception is arrays ( short[] ), where it is always guaranteed that they take up less space than int[] inside). How about Dalvik?

eg. I have a class that contains 50 fields of type short . Sometimes in my application there are 10,000 of these classes. This means that short fields should use 1 MB of memory, but if Dalvik uses 4 bytes for short values ​​inside, then it will use 2 MB of memory.

How much memory should I expect from Dalvik? (This refers to the use of its internal memory, and I know that this may not affect the use of system memory, for example, since Dalvik has already reserved a higher amount of memory from the system.)

+8
java android dalvik
source share
2 answers

In dalvik, double and long fields are 8 bytes, all the rest (including short) are 4 bytes.

Short arrays, on the other hand, take 2 bytes per element (in addition to the front space for maintaining an array + object).

Arrays

new-array opcode calls dvmAllocArrayByClass (line 71) to allocate space. Then it calls dvmAllocPrimitiveArray (line 113). The dvmAllocPrimitiveArray switch uses case "S" for a short array. You can see that it calls allocArray (line 38) with width = 2.

Inside allocArray it calculates the size of the array for the following size:

 size_t elementShift = sizeof(size_t) * CHAR_BIT - 1 - CLZ(elemWidth); size_t elementSize = length << elementShift; size_t headerSize = OFFSETOF_MEMBER(ArrayObject, contents); size_t totalSize = elementSize + headerSize; 

In short, in a 32-bit system this calculation will be:

 size_t elementShift = (4 * 8) - 1 - 30; //== 1; size_t elementSize = length << 1; //ie length * 2 size_t headerSize = <some constant value>; size_t totalSize = length*2 + <some constant value>; 

Short arrays take 2 bytes per element.

Fields

new-instance opcode calls dvmAllocObject (line 181) to allocate space for the new object. The size that is allocated is based on the objectSize ClassObject field. objectSize set to computeFieldOffsets (line 3543). If you find that every instance where fieldOffset is incremented in this function, you will notice that it is always incremented in increments of 4 bytes.

Short fields occupy 4 bytes.

+4
source share

(There will be a comment, but it's too long for that.)

This is a fairly common procedure for 4-byte fields to be used for short local vars, since the JVM is conceptually a 4-byte register machine and with all the other garbage in the frame stack, it does not make much difference.

For example, fields that are likely to depend on tradeoffs with storage preservation, and also to spend cycles expanding and leveling out - frequent cost increases take a small cycle, and even on architectures that are supposedly “agnostic” with regard to border alignment there it’s usually a penalty for access to borders, so “packing” fields without first rebuilding them to preserve word / double word boundaries can cost performance.

So, if the JVM selects a "package", you usually need to reorder the fields. Naive JVMs avoid reordering as it simplifies some aspects of the JVM, but (as one example) in AS / 400 we found that aggressive reordering and instance packing fields received a 30% performance increase for patented memory applications.

I never looked at Dalwick's insides. The standard Sun-based JVMs historically (in spite of everything recently) depended on the layout / order of things in the .class file and, therefore, did not "naturally" succumb to reordering. But Dalvik re-creates the .class file, and therefore it is better suited for reordering instance fields.

Note that to test the hypothesis that Dalvik packs short fields, you would need to create a class with several dozen instance fields, and then determine what the resulting size of the object was. Also (assuming the packaging is visible in the first case), create a class with the short and int (or possibly long ) fields to see if Dalvik will change them to achieve the packaging.

+2
source share

All Articles