wood burning stoves 2.0*
The moose likes Performance and the fly likes will file reading use up jvm or memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "will file reading use up jvm or memory?" Watch "will file reading use up jvm or memory?" New topic
Author

will file reading use up jvm or memory?

Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
Hi,

I'm kind of obsessed with any resource taking up memory or anythign in the jvm if i devlop any custom-based application. if anything wrong happens i'd be blamed for the problem.

This time , i just developed a jsp file that reads a text file (which is a log) and everytime the user refreshes the entire log file is displayed.

My question is: will file reading everytime hinder the performance of the or memory or jvm? i don't think the cpu would be involved.

What i do is just declare a buffered reader, read the file, split it into chunks according to time and then print it with a loop. This will happen everytime the user refreshes.


CHeck out this code: what should i be afraid of?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
My question is: will file reading everytime hinder the performance of the or memory or jvm? i don't think the cpu would be involved.


Obviously, reading a file will use up memory, I/O bandwidth and CPU time. But so will everything else you might do instead (e.g. caching the file contents), so just by that we can't really advise you.

Amongst the questions you should be asking yourself are: How often does this page get accessed? If it's just once an hour or so it doesn't seem worth the effort to worry about this. If it gets accessed every few seconds it would be a different story.

Whether caching will help depends on how often the file changes. How often would the page get accessed between changes? If caching only saves a couple of reads between changes, it's probably not worthwhile doing. On the other hand, if you can serve the cached dozens (or even hundreds) of times between changes, it would be a different story.

If you really wanted to worry about object creation, you could cache the date-related strings (year, month, day, formattedMonth, fileAbsolute), since they only change every 24 hours. You could have a background thread change those automatically at midnight every day. But, again, whether that's worthwhile depends on how often the page is accessed.

From a design point of view, I wouldn't do I/O in a JSP, but use a servlet or backing bean instead. Scriptlets in JSP are considered bad design anyway.


Ping & DNS - my free Android networking tools app
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
hey ulf,

i agree, jsps in files are crappy design. thing is this is i have no control over the application. it's the vendor's application and i'm just trying to make the log more accessible and available to a few operators. So i might let them access it every while (say 15-20 mins) JSP page is kind of the safest way i guess just to keep it in one page and avoid touching the web.xml or any other file.



anyway,

how does caching work?
how do i refresh a page with caching?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Originally posted by Ali Khalfan:
i agree, jsps in files are crappy design. thing is this is i have no control over the application.

You have some control - you are allowed to edit a JSP. Why not create a new jar with your code and have the JSP call that? This approach would allow you to introduce new, clean, well designed code while not requiring other changes to the existing app.


how does caching work?
how do i refresh a page with caching?

Caching is where you store data in memory. You would store it in a static variable and only read from the file after X minutes have passes. (Please, please, please don't do this in a JSP. Code in a JSP is bad enough - static variables are even worse.)

It sounds like there might be some premature optimization going on here. Have you demonstrated a performance problem reading the file each time? How long does that take? How big is the file?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
hi Jeanne,

on any given date i doubt the file will exceed 500 kb. i'm not afraid of the performance of this particular script, but of the entire system ( alot of transactions occur on it)

Let's say i use caching how would i update the date when the new day occurs.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Originally posted by Ali Khalfan:
i'm not afraid of the performance of this particular script, but of the entire system ( alot of transactions occur on it)

You could always use a profiler to see if you have enough CPU or memory to spare. I'm not that clear on why you are considering caching if you have a memory issue - caching uses a lot of memory.


Let's say i use caching how would i update the date when the new day occurs.

A typical cache method looks like:
 
Consider Paul's rocket mass heater.
 
subject: will file reading use up jvm or memory?
 
Similar Threads
Replacing a perticular text in property file keeping other content intact
Do I need to use a stringbuffer?
naima
I want to change this logLine format?
naima