• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about servlets and JVMs

 
liao Yang
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a question about where a servlets are running, i mean in which JVM.

Are they running in a single JVM or one JVM for each servlet? I know this question sounds silly, but the debate on page 28 on the HFSJ book does make me thinking that each servlet is running in its own JVM.

Here is the debate:

...

Performance, for one thing. With Perl, the server has to launch a heavy-weight process for each and every request for that resource.

This is no different from Java... what do you call the JVM? Is not every instance of the JVM a heavy-weight process?

Ah, yes, but you see Servlets stay loaded and client requests f a servlet resource are handled as separate thread of a single running servlet. There's no overhead of starting the JVM, loading the class, and all that...

...

Thanks
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liao, there is only one JVM per jvm. There is 1 instance of every servlet in a web application (unless you use single threaded model). And there is 1 Thread for every request to a servlet...
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is 1 instance of every servlet in a web application (unless you use single threaded model). And there is 1 Thread for every request to a servlet.


Yes this is correct. But web containers actually maintain a pool of servlet instances so as to increase performance, like stateful session beans.
 
liao Yang
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit and Deepak

Thank you for your replies. I know that 1 servlet instance per web app, but this still didn't answer the question of "how amny JVMs".

Ankit, what did you mean by "there is only one JVM per jvm."? I know it's a typo, but what were you trying to say?

Thanks

Liao
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I can see why a book having a conversation with itself is confusing Here's my plain-speak take on what they mean:
With Perl, the server has to launch a heavy-weight process for each and every request for that resource.
A server like Apache forks off a new Perl interpreter for every request; this interpreter is quite bulky, taking CPU time to load and allocate memory. If multiple requests come in, a Perl interpreter system process must be started to handle each one, so you can have many processes started simultaneously. This happens on every request, using the CGI system, and can be quite slow. That's what they're trying to say, but this isn't actually quite true. Modern servers use FastCGI which keeps a pool of Perl interpreters hanging around in memory so they don't have to be started on each request. This makes the process fast, but doesn't help to promote Servlets!
This is no different from Java... what do you call the JVM? Is not every instance of the JVM a heavy-weight process?
Apache is a single Web server which can spawn multiple Perl interpreters. A Java EE server runs in its own JVM, and there is only one and it is started once when the Web or EE server is started. This is just like Apache, and not like the Perl interpreters.
Ah, yes, but you see Servlets stay loaded ... There's no overhead of starting the JVM, loading the class, and all that...
This is saying that once the single server's JVM has started, all Servlets are run in the same JVM. Mostly a container will just create one instance of each Servlet class and keep it hanging around, using that single instance to serve all requests. This means there is little-to-no delay in execution speed from the point the container receives the request to the point the Servlet implementation can process it.

If you compare Servlets to Perl over FastCGI or Apache's mod_perl you don't see much difference in the startup times or memory consumption. However, Servlets can be several times faster in execution---e.g. because Perl doesn't compile into bytecode or keep the code in memory, so has to re-interpret the script each time. Also Perl runs independent memory spaces for each interpreter whereas Servlets can share objects between request in the same session. However, Perl has its uses too... it's string manipulation and regex functions are implemented in highly optimised C so are very fast, typically faster than Java. Since webpages do a lot of string processing, Perl can be almost as fast as Java for these use cases, even with the disadvantages mentioned above.

That should be food for thought anyway! Hope that helps.
[ December 09, 2008: Message edited by: Charles Lyons ]
 
liao Yang
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Charles for the clear explanaion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic