0

If I pass an array of data type double having 100 elements (for example) from an application server's (Linux CentOS) Java (1.6) method to a client using a binary transfer (e.g. AMF protocol), how many bytes are transmitted?

For example, if it were an array of double floating-point numbers, each number occupies 8 bytes. So, if the array is 100 elements long, and each element were null, would an array size of 800 bytes be transmitted? That is, does each element still occupy 8 bytes if it is null?

UPDATE:

Ultimately what I'm getting at with the above question, is I have a (x,y) data set on a server that represents a plot. The y data is unique, but the x-data is equally spaced. So, I can avoid transmitting the x-data from the server to the client (instead transmitting the x-interval and let the client re-create the predictable x-axis values). However, the array being passed from the server to the client appears as:

myArray[0].x = null
myArray[0].y = 1e-302
myArray[1].x = null
myArray[1].y = 1.42e-202
...
myArray[99].x = null
myArray[99].y = 2.3234e-3

and I'm wondering if those null values are going to each be 8 bytes long or if I'll get some memory savings because they are null.

UPDATE 2:

PDF page 82 here for the BlazeDS (e.g. LCDS) documentation describes conversion from Java to ActionScript AMF3 (used by BlazeDS and LCDS). On PDF page 83 the table shows Java type null maps to ActionScript AMF3 type null.

ggkmath
  • 4,188
  • 23
  • 72
  • 129
  • This is highly context-dependent. – obataku Sep 12 '12 at 02:22
  • that is 100% up to the protocol. is it actually AMF or is that just an example? – Affe Sep 12 '12 at 02:22
  • 1
    AMF seems to encode nulls with one byte (0x01, the type byte) and doubles with nine bytes (the type byte followed by the 8-byte double). In that case, you will save a lot by encoding nulls instead of regular data values. – nneonneo Sep 12 '12 at 02:31
  • Thanks nneonneo, is that in the documentation somewhere you could point me to? – ggkmath Sep 12 '12 at 02:35
  • 2
    It's in the Wikipedia article that you linked to in your question. – Ted Hopp Sep 12 '12 at 02:39
  • -1 - The wording of this Question is confusing and self-contradictory. 1) 1t asks about "space occupied" but it is really about bytes transmitted. 2) It asks about binary formats in general, but it is really about a specific format. 3) Then the updates throw in irrelevant data structures and an link to a 6Mb PDF that is barely relevant. – Stephen C Sep 12 '12 at 02:44

2 Answers2

3

In AMF0, according to the Wikipedia article you linked to, a "number" (AMF0's only numeric type, but it corresponds to Java's double) will be nine bytes (0x00 to indicate it's a number, plus eight bytes for the number's double-precision floating-point representation), whereas a null will be just one byte (0x05). An array contains two bytes (initial 0x08 and final 0x09) plus its contents, so if the array has length n, and k of its elements are numbers, then its total size will be 2 + 9 × k + (nk) = 2 + n + 8 × k. As you can see, nulls can potentially reduce the transmission payload significantly.

AMF3 differs in details, but is similar.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Great, thanks. I noticed PDF page 83 in the linked LCDS documentation above shows BlazeDS (e.g. LCDS) maps Java type `null` to AMF3 type `null`, which helps fill in the answer a little, as now the Wikipedia link shows the AMF0 (assumed to be same as AMF3) `null` occupies 1B. – ggkmath Sep 12 '12 at 02:45
2

There is no DOUBLE data type in Java SE. Since it's nullable, then we're talking about Double (and not double). Instances of the wrapper type (Double) use considerably more memory than values of the primitive type (double).

Any null reference will use less memory than the memory consumption of a reference to instance of a class plus the memory used to store the instance itself. You might naively assume that a null reference uses 4 or 8 bytes (depending on whether you're using a 32- or 64-bit JVM) but there are fancy-schmancy optimizatons that the JVM can use to potentially reduce the size of an object reference (more on that here and here).

Either way, if you're so concerned with memory usage, why are you using Double at all, instead of double? If it's just because you need a "no value" value, use Double.NaN.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • He's not asking about memory usage, but about a transmission protocol (AMF). – nneonneo Sep 12 '12 at 02:31
  • The client needs to receive the model structure as shown above, that is, the `myArray[i].x` needs to be present, and in the client it is populated with double. – ggkmath Sep 12 '12 at 02:33