Java: how many bytes needed to transmit double data type array containing nulls?

StackOverflow https://stackoverflow.com/questions/12380413

  •  01-07-2021
  •  | 
  •  

Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top