Each allocated InstanceKlass has a field "_inline_layout_info_array", which is a pointer to an array allocated in Metaspace containing an InlineLayoutInfo for each field in the class. If a field could be chosen to be inlined (i.e., a field is of a value class type), the corresponding index in the _inline_layout_info has been populated with valid data for an InlineLayoutInfo, otherwise the index contains "garbage". InlineLayoutInfo contains the InlineKlass, LayoutKind and null_marker_offset for that specific field.
Right now this array is allocated when the InstanceKlass is parsed in ClassFileParser for all InstanceKlasses, even those who never use it. There are three possible scenarios:
1) There are no fields that could ever be inlined (i.e., likely no value class fields)
2) There are fields that could be inlined (i.e., at least one value class field), but no field is inlined
3) There are fields that could be inline, and at least one is inlined
The only scenario where we need to keep track of the array of InlineLayoutInfo is for scenario 3), where we need this information to operate on the field in the future. For the other two scenarios, there is no need to keep track of this information at all, and is right now wasted space.
The solution I propose to fix this is twofold: First, allocate the array of InlineLayoutInfo on demand, only when we are about to populate the array with something meaningful. Second, after we've chosen a layout, where no fields have been inlined and we have previously allocate the array of InlineLayoutInfo, we deallocate the array and do not transfer ownership of it to the InstanceKlass. There are already code in place to deal with not having the array allocated, likely because the code is supporting a mode where Valhalla is not enabled/used where the array is not allocated at all.
Some sanity testing using NMT shows that the usage of "Metadata" in the "Class" section is reduced by many KBs up to single-digit MBs for simple programs which do not use value classes.
Right now this array is allocated when the InstanceKlass is parsed in ClassFileParser for all InstanceKlasses, even those who never use it. There are three possible scenarios:
1) There are no fields that could ever be inlined (i.e., likely no value class fields)
2) There are fields that could be inlined (i.e., at least one value class field), but no field is inlined
3) There are fields that could be inline, and at least one is inlined
The only scenario where we need to keep track of the array of InlineLayoutInfo is for scenario 3), where we need this information to operate on the field in the future. For the other two scenarios, there is no need to keep track of this information at all, and is right now wasted space.
The solution I propose to fix this is twofold: First, allocate the array of InlineLayoutInfo on demand, only when we are about to populate the array with something meaningful. Second, after we've chosen a layout, where no fields have been inlined and we have previously allocate the array of InlineLayoutInfo, we deallocate the array and do not transfer ownership of it to the InstanceKlass. There are already code in place to deal with not having the array allocated, likely because the code is supporting a mode where Valhalla is not enabled/used where the array is not allocated at all.
Some sanity testing using NMT shows that the usage of "Metadata" in the "Class" section is reduced by many KBs up to single-digit MBs for simple programs which do not use value classes.