Re: String storage
Dra?en Gemi? wrote:
[...]
My customer is a company that is in education business. They have
a number of lecturers and a number of classrooms.
They need a web application to cover their activities. I have written
a HTML interface that represents a month timetable of classes. It is a
web page that represents current month with a few days of next and
previous months. The page shows all the classrooms in one page, as grids
with days in columns and hours of the day in rows. Each cell represend
30 minutes interval.
One month plus a little -- let's say forty days.
Cells at thirty-minute intervals -- forty-eight cells per day.
Not entirely clear about how the classrooms enter into this,
but let's be pessimistic and suppose a separate 40x48 grid for
each of 100 classrooms.
Grand total: 192000 cells (a rather unwieldy Web page, I'd say.)
If each cell had its very own un-shared forty-character String,
they would use a little less than fifteen megabytes of character
data.
Additionally, they wanted lecturer's names as a tooltip (when one drives
mouse within the cell).
So there are many cells, and only a few lecturers.
Each timetable page request is backed by separate object on the server
side, because the customers can choose between the months. The timetable
page is going to be requeste very often, so I need to optimise the
memory usage.
I think you are attacking the problem from the wrong end: You
are worrying about the String objects, but you really ought to be
thinking about the Lecturer objects. Each Lecturer presumably owns
a String with the lecturer's name, so there are only as many of those
Strings as there are Lecturers -- and among that group, there are
probably not very many duplicates. (You may have more than one
"John Smith" on the faculty, but you don't have thousands and
thousands of them.)
When you tell some JThing to use a particular Lecturer's name
String as its tool tip, you are not creating a brand-new String
(well you *could*, but that would be silly). You are just telling
the JThing to use the existing String; if you set the same String
as the tool tip for a hundred different JThings, they all share
the same String object.
I strongly suspect that you are "optimizing" for a problem you
do not even suffer from. That's a waste of your time and energy,
and to the extent that it makes your code more complicated it's
also a threat to maintainability. That's why I'll reiterate my
earlier advice: Do *not* do anything special to deal with the
problem until and unless you have reason to believe the problem
actually exists. Nothing you have presented thus far suggests
that you have a problem.
--
Eric Sosman
esosman@acm-dot-org.invalid