[JAVA] The story of @ViewScoped consuming memory

environment

Web application

phenomenon

The application died in OutOfMemory. Closed screen management beans are alive and R.I.P.

Suspicious part

Initial flow

Common explanation

Annotation Namespace Lifetime
@RequestScoped javax.enterprise.context.RequestScoped One HTTP request/response
@ViewScoped javax.faces.view.ViewScoped While the views are the same
@SessionScoped javax.enterprise.context.SessionScoped Session start-end
@ApplicationScoped javax.enterprise.context.ApplcationScoped While the application is running
@Dependent javax.enterprise.context.Dependent Depends on the scope to which it was injected
@ConversationScoped javax.enterprise.context.ConversationScoped You can specify the start and end arbitrarily
@FlowScoped javax.enterprise.context.FlowScoped From start to end of a predefined flow

My understanding

Hmm. ** It is convenient if @ViewScoped is generated when the screen is opened and discarded when it is closed. ** ** You can give @SessionScoped the information of the logged-in user and create a business screen with @ViewScoped.

You can also specify the number of Views that a session holds in web.xml.

/web.xml
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>sever</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.numberOfLogicalViews</param-name>
        <param-value>20</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.numberOfViewsInSession</param-name>
        <param-value>20</param-value>
    </context-param>

Know if it will be released if the upper limit is exceeded.

When I did something like that, the management bean of @ViewScoped, which should have been destroyed, was so alive that the memory was crushed.

Actually

Apparently, the management bean of @ViewScoped will not be released unless it has the following pattern.

Therefore, it survives in the following pattern.

solution

Use OmniFaces @ViewScoped instead of the standard @ViewScoped. Just use the @ViewScoped annotation of ʻOmniFaces and it will release it when you pick up the ʻunload event of your browser. It is designed so as not to affect the default dependency as much as possible, so I wonder if it can be introduced just to use @ViewScoped.

Afterword

I wondered if View would be managed on a screen-by-screen basis, but it's not a screen-based unit, but a component tree-based unit, so it's wrong to expect that it will be released when the screen is closed.

It is a trap behavior that it is not discarded even if the maximum number of logical views is exceeded.

~~ Don't hold the session for a long time & don't make it huge ~~

References

jsf-2 – Why @ViewScoped Beans that have expired are not destroyed until the session expires stack overflow : JSF 2.2 Memory Consumption: Why does Mojarra keep the ViewScoped Beans of the last 25 Views in Memory?

Recommended Posts

The story of @ViewScoped consuming memory
[Java version] The story of serialization
The story of encountering Spring custom annotation
Examine the memory usage of Java elements
The story of updating SonarQube's Docker Container
The story of RxJava suffering from NoSuchElementException
The story of AppClip support in Anyca
The story of writing Java in Emacs
The story of low-level string comparison in Java
The story of learning Java in the first programming
The story of making the existing Dockerfile GPU compatible
The story of introducing Ajax communication to ruby
The story of raising Spring Boot 1.5 series to 2.1 series
The story of tuning android apps with libGDX
The story of adding the latest Node.js to DockerFile
The story of initializing Money :: Currency when testing
The story of migrating from Paperclip to Active Storage
Judgment of the calendar
The story of an Illegal State Exception in Jetty.
The world of clara-rules (4)
Examine the boundaries of the "32GB memory wall" in Elasticsearch
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
The story of making Dr. Oakid using LINE BOT
The story of making dto, dao-like with java, sqlite
The idea of jQuery
The story of managing session information by introducing gem Redis
The story of raising Spring Boot from 1.5 series to 2.1 series part2
20190803_Java & k8s on Azure The story of going to the festival
A story packed with the basics of Spring Boot (solved)
The story of throwing BLOB data from EXCEL in DBUnit
The story of pushing Java to Heroku using the BitBucket pipeline
The story of dieting the container of the Elixir application as much as possible
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
About the handling of Null
Docker monitoring-explaining the basics of basics-
About the description of Docker-compose.yml
Understand the basics of docker
The play of instantiating java.lang.Void
Explanation of the FizzBuzz problem
The basics of Swift's TableView
Story of touching mailchimp-api v3
Median of the three values
The illusion of object orientation
Switch the version of bundler
The story of not knowing the behavior of String by passing Java by reference
The story of "duplicate symbol" after putting Swifty Tesseract and OpenCV
The story of Collectors.groupingBy that I want to keep for posterity
The story of toString () starting with passing an array to System.out.println
A story about hitting the League Of Legends API with JAVA
A story that struggled with the introduction of Web Apple Pay
The story of making a communication type Othello game using Scala.
The story of stopping because the rails test could not be done
A story that confirmed the profile of Yasuko Sawaguchi 36 years ago
The story that ARM's processing performance of Open JDK was low