aspose file tools*
The moose likes Tomcat and the fly likes Loading data into RAM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Loading data into RAM" Watch "Loading data into RAM" New topic
Author

Loading data into RAM

Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
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.

public void init(ServletConfig config) throws javax.servlet.ServletException {
super.init(config);
try {
ServletContext sc = getServletContext();
if(header == null){
synchronized (sc){
FileReader fr = new FileReader("c:\\bigfile.txt");
char[] cbuf = new char[4460036];
int i = fr.read(cbuf,0,4460036);
fr.close();
header =new String(cbuf);
}//end synchronized
}//end if header

}catch(Exception io){
}
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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
Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
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?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.
Roland gray
Greenhorn

Joined: Nov 28, 2004
Posts: 21
Mike,

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 ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading data into RAM
 
Similar Threads
What values initialized inside init() in servlet
What values initialized in servlet
Servlet does not start when WL 11g is restarted
Servlet Config
HTTP POST body gets truncated