Uploaded image for project: 'MyFaces Core'
  1. MyFaces Core
  2. MYFACES-4433

Memory Leak with ViewScopeBeanHolder's storageMap

    XMLWordPrintableJSON

Details

    Description

      Hello,

      This memory leak issue pertains to CDI ViewScope beans's storage object.  As you recall, ViewScope beans are deleted once the view changes, or the session expires.

      If you look a the code linked, you'll see that when ViewScopeBeanHolder#getContextualStorage is called, a new contextualStorage (to store ViewScoped beans) is created and stored within storageMap .  For every new view created, a new object is created and add this map. 

      https://github.com/apache/myfaces/blob/2.2.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java#L90-L94

      When the session expires, the storageMap is cleared via forceNewStorage.  (via the SessionScoped ViewScopeBeanHolder#destroyBeansOnPreDestroy’s PreDestroy annotation) This is fine.

      https://github.com/apache/myfaces/blob/2.2.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java#L180-L193 

      https://github.com/apache/myfaces/blob/2.2.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeBeanHolder.java#L135

      However, if the view changes, the beans are destroyed, but the contextualStorage (which holds the beans) still exists in the storageMap.  The only things that happens during this handling are:  the context map is cleared and storage is deactivated.  The contextualStorage is not removed, however.

      https://github.com/apache/myfaces/blob/2.2.x/impl/src/main/java/org/apache/myfaces/cdi/view/ViewScopeContextImpl.java#L256-L258

       The memory growth is small, but can still add up over time.  Here's a screenshot of the leak:

      This seems like a bug to me as I don't see any use for the contextualStorage once it's deactivated.  Therefore, I've attached a PR to address this issue.  Thanks!

      Attachments

        1. image-2022-04-27-15-35-40-508.png
          151 kB
          Volodymyr Siedlecki

        Activity

          People

            volosied Volodymyr Siedlecki
            volosied Volodymyr Siedlecki
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: