How garbage collection works with Session object in Java?
How garbage collection works with Session object in JSP? Whether each session gets garbage collected after session gets expired? What is better way to handle开发者_如何学C memory with Session objects in JSP?
The garbage collector will only collect objects which doesn't have any hard references anymore from in the code. The HttpSession
objects are referenced by servletcontainer's internal code. When the session expires, the servletcontainer's internal code will dereference it and the GC will sweep it when it's time.
You don't need to worry about this at all. It's already all done for you.
If your actual problem is the excessive memory usage and/or running out of memory, then the cause of the problem lies definitely somewhere else and has to be solved on a different way. Basically, you should not be referencing more objects than necessary.
A timer is reset every time a session object is accessed or created. To set the value of the timer, you enable the session timeout property.
Eventually the user doesn't connect, and the session timer for that session expires, then it will be removed from the Servlet container's set of sessions and be subject to garbage collection like any other object.
精彩评论