I am using ObjectMapper
from Jackson to convert the Java object to String in order to write it in my log file.
The converting method looks like:
private String getResponseAsString(OrderResponse response) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(response);
} catch (JsonProcessingException e) {
log.error("Error when converting response:" + getExceptionMessage(e));
return "Error when converting response";
}
}
After I ran my program, I saw that it threw the OutOfMemoryError: Java Heap Space
error on this method:
so I think my program is running out of memory. I read some articles that I can overcome this problem by increasing the memory. But right now when I check the settings, the default Xmx in my computer is already nearly 4GB so I don't want to increase it more.
Can anyone give me a hint how can I save memory with this converting Object to String method by replacing it with another method that has the same function? Or does anyone have another idea/approach how to overcome this problem.