Hello, I've used the following init function listed below in my servlets in order to ensure that the file "bigfile.txt" is only loaded into memory and stored in a variable called "head" one time when the servlet is first called. It appears to work because I noticed that the file "bigfile.txt" is only accessed once regardless of how any times I call the servlet.
Question: Being new to TomCat I wanted to ensure that this is indeed all that I needed to do and that the file bigfile.txt is not being rewritting by Tomcat somewhere else on the disk and being loaded everytime I call the servlet. Thus given "me" the illusion that the file " bigfile.txt" is actally in memory.
There is nothing magic about the servlet environment. The contract for the init method is that it will be executed completely before the first request is processed, and that it will be executed only once during the life of the servlet. I'll assume that your real code will catch that IOException and report it if there is a problem opening the file. There is no earthly reason for synchronizing in that code. The servlet container will only devote one Thread to calling init. Bill
Joined: Nov 28, 2004
Are you saying that the file is indeed loaded to ""RAM"" and that it is not stored as some temporary file to disk by tomcat and reloaded when needed?
Well you've read the file in and created a String. At that point, it's a JVM/OS issue whether or not the bits of RAM that contain your string are being written to page file and back out again.
If your only concern is "does this code only run once", then be assured that it will only be run once during the lifetime of the servlet.
If you're being paranoid about performance (reading a file in and out and in and out again...) then even *if* the JVM did that (which it doesn't), there'd be nothing you could do about it.
Joined: Nov 28, 2004
I agreefor the most part with your conclusion. I was simply looking to better understand life after init (the servlet init function). I know that there are many JVM parameters that one can tweak to enhance performance. Perhaps some one is familiar with these settings and how they can better address my performance concerns which appears to be related to optimizing memory management for a given class file in JVM. [ November 30, 2004: Message edited by: Roland gray ]
so you have a memory management problem, and you've tracked it down to one class? You've done this with profilers?
And yes, tuning memory and garbage collection settings ... someone could write a whole book on the topic.
Author and all-around good cowpoke
Joined: Mar 22, 2000
Random material on performance: I like the Java Performance Tuning web site - they aggregate performance related material from all over - including JavaRanch discussions - how circular! Subscribe to their newsletter My favorite Tomcat book - Professional Apache Tomcat 5 (Wrox, 6 authors - gang of six? ) does not have many specific recommendations on performance. Apparently they feel that each web application presents its own optimization problems.
An enjoyable discussion... includes the famous statement "premature optimization is the root of all evil" attributed to CAR Hoare. (it turns up first on a Google search for "premature optimization") Bill