You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cxf.apache.org by "Eric (Jira)" <ji...@apache.org> on 2022/08/04 13:20:00 UTC

[jira] [Updated] (CXF-8745) MemoryLeak when using SpringBus in a spring context which has a reusable parent context

     [ https://issues.apache.org/jira/browse/CXF-8745?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric updated CXF-8745:
----------------------
    Description: 
SpringBus automatically registers itself as an ApplicationListener in the SpringContext and recursively also in all its parents:

 
{code:java}
public void setApplicationContext(ApplicationContext applicationContext) {
        ctx = (AbstractApplicationContext)applicationContext;
        @SuppressWarnings("rawtypes")
        ApplicationListener listener = new ApplicationListener() {
            public void onApplicationEvent(ApplicationEvent event) {
                SpringBus.this.onApplicationEvent(event);
            }
        };
        ctx.addApplicationListener(listener);
        ApplicationContext ac = applicationContext.getParent();
        while (ac != null) {
            if (ac instanceof AbstractApplicationContext) {
                ((AbstractApplicationContext)ac).addApplicationListener(listener);
            }
            ac = ac.getParent();
        } 
}{code}
This leads to a MemoryLeak when the current SpringContext is closed but the ParentContext is reused for another child context, because the ApplicationListener references the SpringBus and the SpringBus references the old ApplicationContext.

 

A very simple approach to solve this problem would be to deregister the listener in a destroy-block or to just use a WeakReference. The later is also used by spring itself when they register a listener in the parent to automatically close all related child contexts.

  was:
SpringBus automatically registers itself as an ApplicationListener in the SpringContext an recursively also in all its parents:

 
{code:java}
public void setApplicationContext(ApplicationContext applicationContext) {
        ctx = (AbstractApplicationContext)applicationContext;
        @SuppressWarnings("rawtypes")
        ApplicationListener listener = new ApplicationListener() {
            public void onApplicationEvent(ApplicationEvent event) {
                SpringBus.this.onApplicationEvent(event);
            }
        };
        ctx.addApplicationListener(listener);
        ApplicationContext ac = applicationContext.getParent();
        while (ac != null) {
            if (ac instanceof AbstractApplicationContext) {
                ((AbstractApplicationContext)ac).addApplicationListener(listener);
            }
            ac = ac.getParent();
        } 
}{code}
This leads to a MemoryLeak when the current SpringContext is closed but the ParentContext is reused for another child context, because the ApplicationListener references the SpringBus and the SpringBus references the old ApplicationContext.

 

A very simple approach to solve this problem would be to deregister the listener in a destroy-block or to just use a WeakReference. The later is also used by spring itself when they register a listener in the parent to automatically close all related child contexts.


> MemoryLeak when using SpringBus in a spring context which has a reusable parent context
> ---------------------------------------------------------------------------------------
>
>                 Key: CXF-8745
>                 URL: https://issues.apache.org/jira/browse/CXF-8745
>             Project: CXF
>          Issue Type: Bug
>          Components: Bus
>    Affects Versions: 3.5.3
>            Reporter: Eric
>            Priority: Minor
>
> SpringBus automatically registers itself as an ApplicationListener in the SpringContext and recursively also in all its parents:
>  
> {code:java}
> public void setApplicationContext(ApplicationContext applicationContext) {
>         ctx = (AbstractApplicationContext)applicationContext;
>         @SuppressWarnings("rawtypes")
>         ApplicationListener listener = new ApplicationListener() {
>             public void onApplicationEvent(ApplicationEvent event) {
>                 SpringBus.this.onApplicationEvent(event);
>             }
>         };
>         ctx.addApplicationListener(listener);
>         ApplicationContext ac = applicationContext.getParent();
>         while (ac != null) {
>             if (ac instanceof AbstractApplicationContext) {
>                 ((AbstractApplicationContext)ac).addApplicationListener(listener);
>             }
>             ac = ac.getParent();
>         } 
> }{code}
> This leads to a MemoryLeak when the current SpringContext is closed but the ParentContext is reused for another child context, because the ApplicationListener references the SpringBus and the SpringBus references the old ApplicationContext.
>  
> A very simple approach to solve this problem would be to deregister the listener in a destroy-block or to just use a WeakReference. The later is also used by spring itself when they register a listener in the parent to automatically close all related child contexts.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)