0

I have an object MyMsg, which one of its properties is a large binary String.

class MyMsg {
    public String moreSmallStuff;

    //May contain a binary String
    public String bin;
}

I am using RabbitMQ to push a msg to a queue so other processes can do their stuff against that new MyMsg object. But the sent message should be in json.

I am having troubles serializing that object with java gson if it contains a large binary string (i.e. +30 MB).

gson.toJson(object);

It fires an OutOfMemoryError exception. So, in order to fix it I have added: -Xmx1024M to the java VM, so it has been fixed in part.

However I really do not like it because it can still crash due to possible bigger files and it seems to me highly inefficient. Is there a better way to parse the object which contains a binary String like that with better performance and memory-use-safer? The binary is just about that size, I cannot believe it requires a 1 GB of RAM to process it.

Thank you very much.

José Cabo
  • 6,149
  • 3
  • 28
  • 39
  • I don't know RabbitMQ but a JSON/HTTP context may be similiar. What you can try is to not hold the complete object in memory before "send" it, but stream it into the "output stream", whatever that is in RabbitMQ. Maybe it could be helpfull if you post the code where you send the JSON string to RabbitMQ. – PeterMmm May 20 '15 at 17:37
  • Dear @PeterMmm, yes it is true that work with an OutputStream would be the ideal, but it does not support them. It forces you to use a byte[]. Because of that, I am trying to optimise the gson process (particularly, the binary string object property). – José Cabo May 21 '15 at 11:54
  • Do you have another suggestion? – José Cabo May 21 '15 at 17:28
  • What is about this one ? http://stackoverflow.com/questions/11660979/using-rabbitmq-to-send-a-message-not-string-but-struct – PeterMmm May 21 '15 at 17:31

0 Answers0