---
title: "Servlet session attribute size and lifetime"  
description: "Servlet session attribute size and lifetime"  
author: "Anonymous User"  
published: 2015-12-03  
updated: 2015-12-03  
canonical: https://www.mindstick.com/forum/33677/servlet-session-attribute-size-and-lifetime  
category: "java"  
tags: ["java", "session"]  
reading_time: 2 minutes  

---

# Servlet session attribute size and lifetime

1) What is an acceptable size range of [objects](https://www.mindstick.com/forum/145447/what-are-objects) that should be attached to a [session](https://www.mindstick.com/articles/12042/session-in-c-sharp)? If I attach an object that can be 5k, and I have 10k valid [sessions](https://answers.mindstick.com/qa/44945/where-do-i-get-information-on-the-sessions-of-lok-sabha) at any given time, that's 50MB of [memory](https://www.mindstick.com/blog/300050/what-causes-sudden-memory-loss) which seems acceptable to me. Is that a valid way to [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) the issue, or are there other significant concerns?\
2) This is probably a dumb [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time), but if I attach an object to a session with a 2 week [lifetime](https://answers.mindstick.com/qa/34025/who-has-been-bestowed-with-the-v-shantaram-lifetime-achievement-award-2018), can I [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) the [attribute](https://www.mindstick.com/blog/221/attributes-reflection) as though it were in memory for the whole [duration](https://answers.mindstick.com/qa/45301/what-is-a-short-duration-discussion) of the session?\
3) Is there any reason NOT to always persist session information to a DB?

## Replies

### Reply by Anonymous User

Acceptable size depends on your use case and number of concurrent sessions. If your sessions last 2 weeks, then it is conceivable that you could have a lot of concurrent sessions, but if 10k is your anticiapted maximum then there should be no problem with a 5k session object (as your calculation illustrates).\
As long as you have a reference to the session object (or are able to get such a reference) then the data will persist in the session. Remember that your session objects must be serializable.\
You may not want to persist all session data to a db for performance reasons, especially if the session data can be easily recreated.


---

Original Source: https://www.mindstick.com/forum/33677/servlet-session-attribute-size-and-lifetime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
