0

In my Controller servlet (Tomcat) I have instantiated an object and have assigned it to a property(p) of the class, like this :

public class Controller extends HttpServlet {

String xmlFile = "/tmp/page.xml";
private Pager p = new Pager(xmlFile);

public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException {

.............

The reason I have done it that way is, there is a lot of cpu intensive hence time consuming tasks done by that instantiation which needs to be done only once (basically it creates all the html page structure of the application).

Now, I use the persistent object(p) and access some methods of it like this :

public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException {
  .................
  .................
  String name = "xxxxx";
  Structure str = p.doAction(name);
  ..................
}

My question is, will the objects, primitives etc created by the methods like getPageName() and doAction() be cleaned up after Tomcat has done with that particular request ? or will it continue to use the memory (same like the persistent object p of Pager) until next restart/shutdown of Tomcat ?

Another important question is, since multiple requests will be handled by the servlet, will there be any issues with the objects and primitives it uses (most of them are local to the method, but some of these variables use properties of the Class) ? like this :

public Structure doAction(String name) {
   if ( pages.containsKey(name) ) {// Here pages is a property of this class (a HashMap)
      return( new Structure( (Structure)pages.get(name) )); //this creates a "deep copy of this object and sends it back...
   }
   return( new Structure() );
}//

As the above method indicates, pages HashMap will be of the form :

page1 => its Structure Object,
page2 => its Structure Ojbect,
..... => .....................,
pageN => its Structure Object

So if the Servlet receives many requests at the same time, for eg: say, there are N number of requests to page2 at a particular time t, will there be any issues accessing the pages HashMap from the doAction() method ? (since its a property of the Class and all these N number of requests will access it (read only, no writing to it) at the same time). I mean, will there be a "read lock" or something ?

Thanks in advance.

M-D
  • 10,247
  • 9
  • 32
  • 35
  • 1
    btw, you should do that initialization in the init() method, so that the framework can properly handle any exceptions. – Zagrev Dec 15 '12 at 06:45

1 Answers1

1

will the objects, primitives etc created by the methods like getPageName() and doAction() be cleaned up after Tomcat has done with that particular request ?

It depends. If these objects/primitives are directly or indirectly referenced by p, then they will be retained for the next requests (term transitively reachable is used sometimes to define this relationship). If these objects are not transitively reachable, they will go away and will be collected by the GC.

since multiple requests will be handled by the servlet, will there be any issues with the objects and primitives it uses (most of them are local to the method, but some of these variables use properties of the Class)<..> ?

Method local objects (if not referenced elsewhere) are thread safe and will not create any problems. Class properties are normally never changing and unless you are modifying them (which would be a weird idea anyway) they should be fine.

if the Servlet receives many requests at the same time, will there be any issues accessing the pages HashMap

See this SO post for more information. The short answer is "most likely it will be OK if you're not modifying the map, but it's higly recommended to use ImmutableMap which is designed for such scenario".

Community
  • 1
  • 1
mindas
  • 26,463
  • 15
  • 97
  • 154
  • Hi mindas, thank you, got some good ideas. I think, the last portion was not very clear, so I have edited it. Can you please have a look at it now ? (only the last question). – M-D Dec 15 '12 at 03:32
  • btw, in the SO link you mentioned, it says that reading from a HashMap is thread safe. – M-D Dec 15 '12 at 04:11
  • Theoretically, if one thread modifies any non-volatile variable in a non-synchronized way, another thread might not see the updated value. As I understand this situation, if one thread initializes the HashMap and others only read from it, this theoretical chance remains. I've seen such scenarios myself where this has caused no problems but why take risks? What if Java runtime behaviour slightly changes in a next version? – mindas Dec 15 '12 at 13:31
  • I'd say if you can precompute the `Structure` object, just do it and store everything in some immutable or at least unmodifiable map. Memory is cheap these days and demand for low latency is high. Or if you want on-demand thread safe computation, look at guava's [LoadingCache](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/LoadingCache.html). – mindas Dec 15 '12 at 13:49