The saved heap size is larger when using jackson ObjectMapper

We use the Jackson utility to convert my Java objects to String, but these objects are not deleted by the GC on the heap.

My code

List<Object[]> reportList; // This contains my objects ObjectMapper map = new ObjectMapper(); // org.codehaus.jackson.map.ObjectMapper return map.writeValueAsString(reportList); 

This returns String to my view level, but objects processed using the mapper object are stored on the heap. I took a bunch of heaps.

 Class Name | Objects | Shallow Heap | Retained Heap ------------------------------------------------------------------ char[] | 5,03,267 | 5,48,74,336 | >= 54,874,336 byte[] | 18,067 | 3,09,01,016 | >= 30,901,016 java.lang.reflect.Method| 2,60,262 | 2,08,20,960 | >= 32,789,040 java.util.HashMap$Entry | 4,31,423 | 1,38,05,536 | >= 92,963,752 java.lang.String | 4,97,172 | 1,19,32,128 | >= 60,889,416 ------------------------------------------------------------------ 

While watching char

 Class Name | Shallow Heap | Retained Heap ----------------------------------------------------------------------------------------------------- [2] char[4][] @ 0x72119e690 | 32 | 5,28,352 '- _charBuffers org.codehaus.jackson.util.BufferRecycler @ 0x72119e658| 24 | 5,28,408 ----------------------------------------------------------------------------------------------------- Class Name | Shallow Heap | Retained Heap ----------------------------------------------------------------------------------------------------- [2] char[4][] @ 0x721158a78 | 32 | 5,28,352 '- _charBuffers org.codehaus.jackson.util.BufferRecycler @ 0x721158a40| 24 | 5,28,408 ----------------------------------------------------------------------------------------------------- Class Name | Shallow Heap | Retained Heap ----------------------------------------------------------------------------------------------------- [2] char[4][] @ 0x7210bc5e0 | 32 | 5,28,352 '- _charBuffers org.codehaus.jackson.util.BufferRecycler @ 0x7210bc5a8| 24 | 5,28,408 ----------------------------------------------------------------------------------------------------- 

How to clear these objects from the heap, is there any kind of cleaning method.

+5
source share
1 answer

The memory usage that you are observing is related to buffer recycling, which uses per-thread SoftReference to store a pair of parsing buffers (a byte[] one, another char[] ). They will be restored if there is a memory pressure; but as long as there are a lot of heaps, they are saved and reused. This can be a significant performance improvement since such buffers do not need to be allocated, flushed, or GC'ed.

So this should not be a problem - it is like disk caching, which the OS does, using memory blocks to cache the disk when there is spare memory.

+1
source

Source: https://habr.com/ru/post/1209525/


All Articles