You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by pedro salazar <pe...@ptinovacao.pt> on 2001/07/06 11:01:37 UTC

creating a instance of a servlet: takes too long!!

Greetings,

why is that my servlet when the first time is invoked, it takes about 30
seconds or more to start when the servlets that came in tomcat are
instantaneous? After the servlet container instantiated and initialized my
servlet, all the following requests are very fast. Is there any advice for
what we should do and don't do in init() method? I just initialize some
properties and a connection pool....
Well is it possible that my servlet container at any time my may shutdown my
servlet to release memory, and another time it will be requested to start
again and take another time too long to start, correct?

How can I benchmark the time of instantiation of my servlet and the time of
my init method?

System configuration:
-Tomcat 3.2.1
-JDK 1.3
-Linux RedHat 6.2 [kernel 2.2.18]
-PII400Mhz 256Mbytes

thanks.
--
<psalazar/>




Re[2]: creating a instance of a servlet: takes too long!!

Posted by wi...@mailops.com.

Friday, July 06, 2001, 10:16:05 AM, pdavison@channelwave.com wrote:

PD> There's another reason for this and it has to do with the java.security.SecureRandom class.
>>>From what I can tell, tomcat uses this class to generate a seed value for the
PD> session ID.  The first request for a SecureRandom value (eg. new SecureRandom().nextLong()) can take many, many seconds to complete.  After the
PD> first usage however, it returns immediately.  I'm not sure what goes on when
PD> SecureRandom initializes but that seems to be where the slow down occurs.

Yes, good point. I've just been looking at that, too. It reminds me of the time
required to create a secure socket factory when using JSSE. After that
time is initially spent, any other new sockets are created quickly.

For development, I change to using randomClass="java.util.Random"

..and have noticed that the attribute randomFile="/dev/urandom" is irrelevant
 on win '98 (I assume this file contains a seed for the random generation?)

Also, there seems to be a lot elsewise going on under the hood. When accessing
a jsp, Tomcat seems to me (at this point) to be compiling servlets in the
webapp. Can anybody elaborate on that?

PD> As a work around, you could create a low priority thread that instantiates a
PD> SecureRandom calls nextLong() then exits, and have this thread get launched by
PD> the init() method of a servlet that gets preloaded.  This solution worked for me.

PD> Regards,
PD> Pete.

PD> Thus spake "pedro salazar" <pe...@ptinovacao.pt> on Fri, 6 Jul 2001 10:01:37 +0100:

PS>> Greetings,
PS>> 
PS>> why is that my servlet when the first time is invoked, it takes about 30
PS>> seconds or more to start when the servlets that came in tomcat are
PS>> instantaneous? After the servlet container instantiated and initialized my
PS>> servlet, all the following requests are very fast. Is there any advice for
PS>> what we should do and don't do in init() method? I just initialize some
PS>> properties and a connection pool....
PS>> Well is it possible that my servlet container at any time my may shutdown my
PS>> servlet to release memory, and another time it will be requested to start
PS>> again and take another time too long to start, correct?
PS>> 
PS>> How can I benchmark the time of instantiation of my servlet and the time of
PS>> my init method?
PS>> 
PS>> System configuration:
PS>> -Tomcat 3.2.1
PS>> -JDK 1.3
PS>> -Linux RedHat 6.2 [kernel 2.2.18]
PS>> -PII400Mhz 256Mbytes
PS>> 
PS>> thanks.
PS>> --
PS>> <psalazar/>
PS>> 
PS>> 



Re: creating a instance of a servlet: takes too long!!

Posted by Peter Davison <pd...@channelwave.com>.
There's another reason for this and it has to do with the java.security.SecureRandom class.
>From what I can tell, tomcat uses this class to generate a seed value for the
session ID.  The first request for a SecureRandom value (eg. new SecureRandom().nextLong()) can take many, many seconds to complete.  After the
first usage however, it returns immediately.  I'm not sure what goes on when
SecureRandom initializes but that seems to be where the slow down occurs.

As a work around, you could create a low priority thread that instantiates a
SecureRandom calls nextLong() then exits, and have this thread get launched by
the init() method of a servlet that gets preloaded.  This solution worked for me.

Regards,
Pete.

Thus spake "pedro salazar" <pe...@ptinovacao.pt> on Fri, 6 Jul 2001 10:01:37 +0100:

PS> Greetings,
PS> 
PS> why is that my servlet when the first time is invoked, it takes about 30
PS> seconds or more to start when the servlets that came in tomcat are
PS> instantaneous? After the servlet container instantiated and initialized my
PS> servlet, all the following requests are very fast. Is there any advice for
PS> what we should do and don't do in init() method? I just initialize some
PS> properties and a connection pool....
PS> Well is it possible that my servlet container at any time my may shutdown my
PS> servlet to release memory, and another time it will be requested to start
PS> again and take another time too long to start, correct?
PS> 
PS> How can I benchmark the time of instantiation of my servlet and the time of
PS> my init method?
PS> 
PS> System configuration:
PS> -Tomcat 3.2.1
PS> -JDK 1.3
PS> -Linux RedHat 6.2 [kernel 2.2.18]
PS> -PII400Mhz 256Mbytes
PS> 
PS> thanks.
PS> --
PS> <psalazar/>
PS> 
PS> 

Re: creating a instance of a servlet: takes too long!!

Posted by wi...@mailops.com.
If your servlet has changed and needs to be recompiled, then using
jikes instead of javac will save a lot of time.

Friday, July 06, 2001, 5:01:37 AM, pedro-b-salazar@ptinovacao.pt wrote:

ps> Greetings,

ps> why is that my servlet when the first time is invoked, it takes about 30
ps> seconds or more to start when the servlets that came in tomcat are
ps> instantaneous? After the servlet container instantiated and initialized my
ps> servlet, all the following requests are very fast. Is there any advice for
ps> what we should do and don't do in init() method? I just initialize some
ps> properties and a connection pool....
ps> Well is it possible that my servlet container at any time my may shutdown my
ps> servlet to release memory, and another time it will be requested to start
ps> again and take another time too long to start, correct?

ps> How can I benchmark the time of instantiation of my servlet and the time of
ps> my init method?

ps> System configuration:
ps> -Tomcat 3.2.1
ps> -JDK 1.3
ps> -Linux RedHat 6.2 [kernel 2.2.18]
ps> -PII400Mhz 256Mbytes

ps> thanks.
ps> --
ps> <psalazar/>



Re: creating a instance of a servlet: takes too long!!

Posted by wi...@mailops.com.

Friday, July 06, 2001, 5:01:37 AM, pedro-b-salazar@ptinovacao.pt wrote:

ps> Greetings,

ps> why is that my servlet when the first time is invoked, it takes about 30
ps> seconds or more to start when the servlets that came in tomcat are
ps> instantaneous? After the servlet container instantiated and initialized my
ps> servlet, all the following requests are very fast. Is there any advice for
ps> what we should do and don't do in init() method? I just initialize some
ps> properties and a connection pool....
ps> Well is it possible that my servlet container at any time my may shutdown my
ps> servlet to release memory, and another time it will be requested to start
ps> again and take another time too long to start, correct?

ps> How can I benchmark the time of instantiation of my servlet and the time of
ps> my init method?

ps> System configuration:
ps> -Tomcat 3.2.1
ps> -JDK 1.3
ps> -Linux RedHat 6.2 [kernel 2.2.18]
ps> -PII400Mhz 256Mbytes

ps> thanks.
ps> --
ps> <psalazar/>



Re: creating a instance of a servlet: takes too long!!

Posted by Bo Xu <bo...@cybershop.ca>.
Bo Xu wrote:

> pedro salazar wrote:
>
> > Greetings,
> >
> > why is that my servlet when the first time is invoked, it takes about 30
> > seconds or more to start when the servlets that came in tomcat are
> > instantaneous? After the servlet container instantiated and initialized my
> > servlet, all the following requests are very fast. Is there any advice for
> > what we should do and don't do in init() method? I just initialize some
> > properties and a connection pool....
> > Well is it possible that my servlet container at any time my may shutdown my
> > servlet to release memory, and another time it will be requested to start
> > again and take another time too long to start, correct?
> >
> > How can I benchmark the time of instantiation of my servlet and the time of
> > my init method?
> >
> > System configuration:
> > -Tomcat 3.2.1
> > -JDK 1.3
> > -Linux RedHat 6.2 [kernel 2.2.18]
> > -PII400Mhz 256Mbytes
> >
> > thanks.
> > --
> > <psalazar/>
>
> Hi :-)  one of the ways is like the following:
>
> in WEB-INF/web.xml, add the following into the servlet-declaration of
> MyServlet:
> ...
> <load-on-startup>1</load-on-startup>
> ...
>
> now MyServlet will be loaded/initialized when TC startup.
>
> Bo
> June 06, 2001

Hi :-)  I forgot the following:
with TC4.0-b5,  when "<load-on-startup>1</load-on-startup>", I remember:
- MyServlet is loaded, i.e.: One Class object of MyServlet is made
- One instance of MyServlet is made, this instance is for that sevlet-declaration
   which you set "<load-on-startup>1</load-on-startup>" for
- init(...) method of this instance is invoked, then, for example, your
DBconnectionPool
   is initialized

  But:  you must use That servlet-name/servlet-mapping to invoke MyServlet,
otherwise,
  another instance of MyServlet will be made, and its init(...) will Still be
invoked,
  i.e.: "the first time" will Still be slow.

and it is possible that container will invoke int(...)/destroy() more than one
time.


Bo
July 06, 2001




Re: creating a instance of a servlet: takes too long!!

Posted by Bo Xu <bo...@cybershop.ca>.
pedro salazar wrote:

> Greetings,
>
> why is that my servlet when the first time is invoked, it takes about 30
> seconds or more to start when the servlets that came in tomcat are
> instantaneous? After the servlet container instantiated and initialized my
> servlet, all the following requests are very fast. Is there any advice for
> what we should do and don't do in init() method? I just initialize some
> properties and a connection pool....
> Well is it possible that my servlet container at any time my may shutdown my
> servlet to release memory, and another time it will be requested to start
> again and take another time too long to start, correct?
>
> How can I benchmark the time of instantiation of my servlet and the time of
> my init method?
>
> System configuration:
> -Tomcat 3.2.1
> -JDK 1.3
> -Linux RedHat 6.2 [kernel 2.2.18]
> -PII400Mhz 256Mbytes
>
> thanks.
> --
> <psalazar/>

Hi :-)  one of the ways is like the following:

in WEB-INF/web.xml, add the following into the servlet-declaration of
MyServlet:
...
<load-on-startup>1</load-on-startup>
...

now MyServlet will be loaded/initialized when TC startup.


Bo
June 06, 2001