[ 
https://issues.apache.org/jira/browse/SLING-7969?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17502181#comment-17502181
 ] 

Sagar Miglani edited comment on SLING-7969 at 3/8/22, 7:23 AM:
---------------------------------------------------------------

Hi [~marett],

I checked the memory dumps of few AEM instances, and here are the findings:

*1)* In an instance having johnzon lib version < 1.1.4, I found that 
DiscoveryLiteDescriptor having retained memory size of ~40MBs, this big memory 
reference is stored when close method of the 
[JsonStreamParserImpl|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java#L921]
 is called. [Close method 
stores|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategy.java#L241]
 the released buffer in a queue (which is default BufferStrategy in Johnzon) 
for [future 
re-use|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategy.java#L232].
 And because default buffer size(length) was 10MB before johnzon 1.1.4, each 
stored buffer will take ~20MB

Also note that the close method was interally being called [after reading the 
json|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderImpl.java#L65]
 in older versions of johnzon. So, DiscoveryLiteDescriptor whether called close 
or not, buffer was still getting released (stored in queue).

!Screenshot 2022-03-07 at 12.26.11 PM.png!

*2)* When I tried the same with johnzon lib version > 1.1.4 (used 1.2.8, 
sling-commons-johnzon is already available with this new version), I found that 
the memory footprint was much low. Also, In this version of johnzon close 
method was not being called by the JsonReader after reading, so buffer was 
never being released (stored in the queue) and while creating the new 
JsonReader, always [new buffer was 
created|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategy.java#L234].
 *But I believe we should close the JsonReader as you provided in this 
[patch|https://github.com/tmaret/sling-org-apache-sling-discovery-commons/commit/edba8e9d8a5ec21f6a95945440e9c9825f00e666]
 (to clear the resources), WDYT? Can you please provide the fix in new release 
of Discovery Commons?*

Dump ss without the 
[patch|https://github.com/tmaret/sling-org-apache-sling-discovery-commons/commit/edba8e9d8a5ec21f6a95945440e9c9825f00e666]:
!Screenshot 2022-03-07 at 1.05.33 PM.png!


Dump ss with the 
[patch|https://github.com/tmaret/sling-org-apache-sling-discovery-commons/commit/edba8e9d8a5ec21f6a95945440e9c9825f00e666]:

!Screenshot 2022-03-07 at 1.14.57 PM.png!

*3)* Apart from the above, I found that an instance of older version of 
JsonProviderImpl was present which was causing the similar problem and I belive 
that should be coming from the sling-installer-core version 3.9.0 (present in 
my instance) which was 
[embedding|https://github.com/apache/sling-org-apache-sling-installer-core/blob/org.apache.sling.installer.core-3.9.0/pom.xml#L63]
 Johnzon core. Newer version (3.11.0+) of sling-installer-core should not have 
this issue.
!Screenshot 2022-03-07 at 1.18.16 PM.png!
!Screenshot 2022-03-07 at 1.18.07 PM.png!


was (Author: sagarmiglani):
Hi [~marett],

I checked the memory dumps of few AEM instances, and here are the findings:

*1)* In an instance having johnzon lib version < 1.1.4, I found that 
DiscoveryLiteDescriptor having retained memory size of ~40MBs, this big memory 
reference is stored when close method of the 
[JsonStreamParserImpl|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java#L921]
 is called. [Close method 
stores|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategy.java#L241]
 the released buffer in a queue (which is default BufferStrategy in Johnzon) 
for [future 
re-use|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategy.java#L232].
 And because default buffer size(length) was 10MB before johnzon 1.1.4, each 
stored buffer will take ~20MB

Also note that the close method was interally being called [after reading the 
json|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderImpl.java#L65]
 in older versions of johnzon. So, DiscoveryLiteDescriptor whether called close 
or not, buffer was still getting released (stored in queue).

!Screenshot 2022-03-07 at 12.26.11 PM.png!

*2)* When I tried the same with johnzon lib version > 1.1.4 (used 1.2.8, 
sling-commons-johnzon is already available with this new version), I found that 
the memory footprint was much low. Also, In this version of johnzon close 
method was not being called by the JsonReader after reading, so buffer was 
never being released (stored in the queue) and while creating the new 
JsonReader, always [new buffer was 
created|https://github.com/apache/johnzon/blob/v1.1.0/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategy.java#L234].
 *But I believe we should close the JsonReader as you provided in this 
[patch|https://github.com/tmaret/sling-org-apache-sling-discovery-commons/commit/edba8e9d8a5ec21f6a95945440e9c9825f00e666],
 WDYT? Can you please provide the fix in new release of Discovery Commons?*

Dump ss without the 
[patch|https://github.com/tmaret/sling-org-apache-sling-discovery-commons/commit/edba8e9d8a5ec21f6a95945440e9c9825f00e666]:
!Screenshot 2022-03-07 at 1.05.33 PM.png!


Dump ss with the 
[patch|https://github.com/tmaret/sling-org-apache-sling-discovery-commons/commit/edba8e9d8a5ec21f6a95945440e9c9825f00e666]:

!Screenshot 2022-03-07 at 1.14.57 PM.png!

*3)* Apart from the above, I found that an instance of older version of 
JsonProviderImpl was present which was causing the similar problem and I belive 
that should be coming from the sling-installer-core version 3.9.0 (present in 
my instance) which was 
[embedding|https://github.com/apache/sling-org-apache-sling-installer-core/blob/org.apache.sling.installer.core-3.9.0/pom.xml#L63]
 Johnzon core. Newer version of sling-installer-core should not have this issue.
!Screenshot 2022-03-07 at 1.18.16 PM.png!
!Screenshot 2022-03-07 at 1.18.07 PM.png!

> Memory leak in DiscoveryLiteDescriptor
> --------------------------------------
>
>                 Key: SLING-7969
>                 URL: https://issues.apache.org/jira/browse/SLING-7969
>             Project: Sling
>          Issue Type: Bug
>          Components: Discovery
>    Affects Versions: Discovery Commons 1.0.20
>            Reporter: Timothee Maret
>            Assignee: Timothee Maret
>            Priority: Major
>              Labels: discovery
>         Attachments: Screenshot 2022-03-07 at 1.05.33 PM.png, Screenshot 
> 2022-03-07 at 1.14.57 PM-1.png, Screenshot 2022-03-07 at 1.14.57 PM.png, 
> Screenshot 2022-03-07 at 1.18.07 PM.png, Screenshot 2022-03-07 at 1.18.16 
> PM.png, Screenshot 2022-03-07 at 12.26.11 PM.png, 
> org.apache.sling.commons.johnzon-1.1.11-SLING-7969.jar, 
> org.apache.sling.discovery.commons-1.0.23-SLING-7969.jar
>
>
> As identified in [~volteanu]'s adaptTo 
> [presentation|https://adapt.to/2018/en/schedule/sling-memory-deep-dive.html], 
> it seems that Sling Discovery on Oak is consuming 42MB of RAM on a standard 
> instance.
> Sling discovery deals with transient states (the views, leases, etc.) and is 
> not caching significant amount of data. The 42MB figure for the discovery 
> feature seems like a symptom of a memory leak.
> [~volteanu] shared in his presentation that the 42MB worth of RAM was mainly 
> consumed by a Json Factory reference. There is one static JsonReaderFactory 
> in the discovery commons module, in the 
> [DiscoveryLiteDescriptor|https://github.com/apache/sling-org-apache-sling-discovery-commons/blob/master/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/DiscoveryLiteDescriptor.java].
> Looking at the code, it seems that each invocation of the 
> [getDescriptorFrom|https://github.com/apache/sling-org-apache-sling-discovery-commons/blob/master/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/DiscoveryLiteDescriptor.java#L51]
>  method creates a JSON reader but never close it. This may may leave 
> resources behind as hinted by the description of the 
> [close|https://docs.oracle.com/javaee/7/api/javax/json/JsonReader.html#close--]
>  method in the API. AFAIK, the 
> [getDescriptorFrom|https://github.com/apache/sling-org-apache-sling-discovery-commons/blob/master/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/DiscoveryLiteDescriptor.java#L51]
>  is invoked periodically on a relatively high frequency (< seconds) which may 
> be the trigger for the leak on all instances.
> This is only a supposition for now, it should be investigated further, simply 
> by running a patched version of \{{org.apache.sling.discovery.commons}} that 
> make sure each JSON reader is properly closed.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to