May have a memory leak in Java webapp - have question about garbage collection and session attributes -
i've been profiling webapp locally on pc visualvm. i'm pretty have small memory leak. after taking snapshot of application, chose object had instantiated objects , looked through allocation call tree see if find class (of mine) contributing 'potential leak'.
i found 3 of classes in tree, , took @ method's pinpointed.
here snippet of code (a method) 1 of servlets - method gets names of session attributes want keep in session , removes rest.
public void dopost(httpservletrequest req, httpservletresponse res) throws servletexception, ioexception { connection conn = null; statement stmt = null; resultset rs = null; try { conn = ds.getconnection(); stmt = conn.createstatement(); httpsession session = req.getsession(); getexemptsessionattributes(customer_number,rs,stmt,session); }//try catch (exception e) { } { if (rs != null) { try { rs.close(); } catch (sqlexception e) { ; } } if (stmt != null) { try { stmt.close(); } catch (sqlexception e) { ; } } if (conn != null) { try { conn.close(); } catch (sqlexception e) { ; } } }//finally }//post public void getexemptsessionattributes(int customer_number, resultset rs, statement stmt, httpsession session) { try { rs = stmt.executequery("select name exemptsessionattributes"); string[] exemptattributes = null; int count = 0; while(rs.next()) { count++; } rs.beforefirst(); exemptattributes = new string[count]; count = 0; while(rs.next()) { exemptattributes[count] = rs.getstring(1); count++; } session.setattribute("exemptsessionattributes",exemptattributes); //garbage collect exemptattributes = null; }//try catch(exception e) {} }//end //....
the modification i've made far, since profiling webapp, addition of setting exemptattributes[] object array null.
my question - if string array (or object) set session attribute, mean reference object, if not set null in code, still 'referenced' , won't garbage collected?
if string array (or object) set session attribute, mean reference object, if not set null in code, still 'referenced' , won't garbage collected?
not exactly.
if have set object session attribute, object continue reachable (and not garbage collected) long session
object reachable. session
object cached in memory servlet infrastructure long time.
in case, assigning null
local variable nothing useful. firstly, local go out of scope anyway, , has same effect gc perspective. secondly, you've copied object reference data structure (the session
object) has longer lifetime local variable ... object continue reachable anyway.
in short, object continues reachable irrespective of whether (or not) assign null
local variable.
possible way combat "leak" include:
- don't put reference object session attribute,
- set session attribute
null
, or - adjust web container's session caching strategy idle sessions dropped cache.
- implement scheme invalidation sessions (e.g. log users out) after period of idleness.
Comments
Post a Comment