Regarding JsonObject and LinkedHashMap implementation

133 views
Skip to first unread message

Yeikel

unread,
Jan 24, 2022, 11:20:26 AM1/24/22
to vert.x
Hi team, 

I just took a memory dump of my application and I noticed that there is a considerable waste of memory due to the LinkedHashMap that backs JsonObjects. 

From the public documentation : 

/**
* Constructs an empty insertion-ordered {@code LinkedHashMap} instance
* with the default initial capacity (16) and load factor (0.75).
*/
public LinkedHashMap() {
super();
accessOrder = false;
}


This usually leads to objects such as : 

LinkedHashMap(size: 2, capacity: 4) {(key:"email", val:"em...@example.com"), (key:"standard", val:"Y")}

j.u.LinkedHashMap<String, ?>(size: 3, capacity: 16) {(key:"xxx", val:j.u.LinkedHashMap(size : 2, modCount : 2, threshold : 3, ...)), (key:"id", val:"123"), (key:"conf_cd", val:"LOW")}

I understand that this is how Java collection works, but is there anything we can do? 

I was thinking that perhaps we could add a new constructor of JsonObject that allows configuring the  initialCapacity and loadFactor but I do not think that this will fix the problem permanently as there are many other places that we use JsonObject. 

Adding a default capacity in the original constructor may lead to considerable CPU waste due to the resizing needed. 


Thomas SEGISMONT

unread,
Jan 28, 2022, 8:56:47 AM1/28/22
to ve...@googlegroups.com
There's no silver bullet for sizing, depends on the application.

To give you maximum flexibility there is a special constructor of JsonObject which takes a Map parameter: io.vertx.core.json.JsonObject#JsonObject(java.util.Map<java.lang.String,java.lang.Object>)

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/47540b92-1eb6-4e97-bbbe-45f2d77faff9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages