skip to Main Content

I would like to ask you something about ObjectMapper.readvalue().

ObjectMapper mapper = new ObjectMapper();

List<ConvertType> responseList = new ArrayList<>();
for (ExampleContent content : iterable) {
    String json = new String(content.getData(), StandardCharsets.UTF_8);
    responseList.add(mapper.readValue(json, ConvertType));

Like the code above, I want to deserialize and save the json data to the list.

However, when I execute the code repeatedly, the direct buffer memory usage increases. Sometimes, Out of direct buffer memory problem occurs.

Does deserializing using ObjectMapper allocates direct buffer memory rather than heap memory?
If so, How can I reduce the amount of direct buffer memory used?

2

Answers


  1. The ObjectMapper uses heap memory for deserialization, not direct buffer memory. Direct memory is primarily used for IO operations.

    I suggest using profiling tools to identify the source that causes the direct memory buffer to increase which may stem from memory leaks elsewhere in the application. Additionally, ensure that a single instance of ObjectMapper is used to reduce the overheard of creating new instances.

    Login or Signup to reply.
  2. In Java, when deserializing data using Jackson’s ObjectMapper.readValue() method, the deserialization process typically involves allocating objects in heap memory rather than direct buffer memory.

    Here’s a breakdown:

    1. Heap Memory: This is where objects are typically allocated in Java. When you deserialize data using ObjectMapper.readValue(), Jackson creates Java objects to hold the deserialized data, and these objects are allocated in heap memory.

    2. Direct Buffer Memory: Direct buffer memory is different from heap memory. It’s used when working with I/O operations, often for performance reasons. Direct buffers are allocated outside of the normal garbage-collected heap, using native memory. However, in the context of deserialization with Jackson, direct buffer memory is not typically used unless there are specific optimizations or customizations made by the developer.

    While Jackson may use buffers internally during the deserialization process for efficient handling of data, the end result (the Java objects holding deserialized data) is usually allocated in heap memory.

    If you have specific requirements for memory management or performance considerations, you might explore options like configuring Jackson’s ObjectMapper to use specific buffer strategies or considering alternatives like manual memory management, but these are more advanced topics and not the default behavior of Jackson’s deserialization process.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search