Memory leak is because of internal implementation in Elcipse xsd framework and logic dealing with XML schemas in GeoServer.
FeatureTypeSchemaBuilder is a singleton component caching GML schema XSDSchema object. At the same time many XSDSchema and XSDElementDeclaration objects are created only for the time of the particular standalone request like DescribeFeatureType request.
In buildSchemaContent(..) method we bind together temporary XSDElementDeclaration object with permanently cached XSDElementDeclaration from gmlSchema related to "gml:_Fetaure" by calling the following:
This internally causes an inverted caching of element in substitutionGroup property of an object that is returned by schema.resolveElementDeclaration(gmlNamespace, substitutionGroup).
The patch demonstrating a workaround to this problem is attached. I do not know deeply a GeoServer codebase how this hack can affect other use cases then just DescribeFeatureType request, but I have tested various requests and everything works well and memory is not leaked anymore.
We need to have "gml:_Feature" element being set in substitutionGroupAffiliation property of a target element for printing a substituionGroup attribute in output XML schema:
... <xsd:element name="a4224al" substitutionGroup="gml:_Feature" type="paikkaoppi:a4224alType" /> ..