I'm trying to send an array list of 1000 long numbers with 19 digits using protobuf. I have used repeated fixed64 as the datatype as it is recommended for numbers often bigger than 2^56 and it is using fixed encoding of 8 bytes. My proto format is
message ListLong {
repeated fixed64 value = 1;
}
I created the proto object using the following method.
static ListLong createListOfLong(List<Long> longList) {
ListLong.Builder list = ListLong.newBuilder();
for(long value : longList) {
list.addValue(value);
}
return list.build();
}
I serialized this message of 1000 long values and converted to byte array. The size of byte array is 8004 bytes.I measured the processing time, CPU usage time and memory allocated using the following methods before and after object creation and serialization.
ThreadMXBean tmx = (ThreadMXBean) ManagementFactory.getThreadMXBean();
Long cpuStartTime = tmx.getCurrentThreadCpuTime();
Long startMemory = tmx.getThreadAllocatedBytes(Thread.currentThread().getId());
Long startTime = System.currentTimeMillis();
Long protoList = createListOfLong(collectionList);
System.out.println("Cpu usage time for proto creation "+(tmx.getCurrentThreadCpuTime()-cpuStartTime)/1000000+"ms");
System.out.println("Heap Memory usage for proto creation "+(tmx.getThreadAllocatedBytes(Thread.currentThread().getId())-startMemory)/1000+"KB");
System.out.println("object creation time is "+(System.currentTimeMillis()-startTime)+" ms");
startMemory = tmx.getThreadAllocatedBytes(Thread.currentThread().getId());
cpuStartTime = tmx.getCurrentThreadCpuTime();
startTime = System.currentTimeMillis();
byte[] b = protoList.toByteArray();
System.out.println("Heap Memory usage for serializing "+(tmx.getThreadAllocatedBytes(Thread.currentThread().getId())-startMemory)/1000+"KB");
System.out.println("Serialized time is "+(System.currentTimeMillis()-startTime)+" ms");
System.out.println("Cpu usage time for serializing "+(tmx.getCurrentThreadCpuTime()-cpuStartTime)/1000000+"ms");
I got the following parameters when using in intellij ide.
Cpu usage time for proto creation 54ms
Heap Memory usage for proto creation 5756KB
object creation time is 56 ms
Heap Memory usage for serializing 3090KB
Serialized time is 29 ms
Cpu usage time for serializing 28ms
Serialized length is is 8004
My questions are
Is there any alternate message format to send an
ArrayList
of long with 19 digits?Can we achieve a byte array length less than 8k by any way?
Why protobuf object creation takes much processing time and memory?
Is there any way to reduce the processing time and memory allocated?