• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Performance question based on two cases

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating a large website and need to optimize it as much as possible. The site will allow my users to log in and will require that I store their user information in a bean which will be stored in their session. With thousands of users logged in at a time, which of the following two cases would be better. (Speed performance along with memory usage). Are there any other variables that I might not be taking into consideration?
Please keep in mind that the actual bean that will hold the user information will contain many more fields and much more data than just the birthday.
Class: SpeedTest

Class: SpeedTest1 (Used with static methods in SpeedTestHelper to save on memory usage and garbage collection cycles)

Class: SpeedTestHelper

Class: SpeedTest2 (Slightly faster but when thousands are created to house every user logged in might take up much more memory and causing more garbage collection cycles)
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe SpeedTest2 instances will take up any more memory than SpeedTest1 instances, as they both hold exactly the same amount of data - in this case, one time stamp. The extra functions should not add to the instance data, since all that has to be attached to each instance is an identifier of what class it is, enabling a lookup to a single set of functions serving all instances.
SpeedTest2 is also a bit cleaner, since it encapsulates all the related functions in one class, and according to your tests, faster. I think that would make it the winner.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as you already have a nice little test program, you could simply modify it to fill an array of 1000 objects and *measure* memory usage, couldn't you?

Originally posted by Chris Davis:
I'm creating a large website and need to optimize it as much as possible.


With all due respect, that is quite unlikely. Much more likely there is some balance between the performance you want to have and the cost of getting that performance. That's why you need to find the spots in your software where you can get the most performance improvement for the least effort. The above probably isn't that place...
 
Chris Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, thank you both for your time and responses. I guess my biggest concern is this. Lets say I have this one class that is used to hold the users data. In this class I add many methods that alter the data, etc that brings the compiled class size up to around 120KB (Without the user data actually in it). I then place this class in the session each time a user logs in for every user. Now the site will obviously need to do more than just allow users to log in, so I�m likely at some point to need other classes stored in the session per each user.
At what point is it worth it to create classes with static methods that I can keep out of the session and use to alter the data of the classes that are in the session, or am I really reading into it more than I should? Also, if this is something to be concerned with, is there a better way of going about it?
Thanks again,
Chris
 
Chris Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Warren Dew:
I don't believe SpeedTest2 instances will take up any more memory than SpeedTest1 instances, as they both hold exactly the same amount of data - in this case, one time stamp. The extra functions should not add to the instance data, since all that has to be attached to each instance is an identifier of what class it is, enabling a lookup to a single set of functions serving all instances.
SpeedTest2 is also a bit cleaner, since it encapsulates all the related functions in one class, and according to your tests, faster. I think that would make it the winner.


Ahhh.. I think I understand what you�re saying. So, even though the actually compiled class size is, lets say, 120KB, even if I �place that class in the session� 1000+ times the memory used is only 120KB (For the class) + whatever data I have in each class?
-Chris
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Davis:

Ahhh.. I think I understand what you�re saying. So, even though the actually compiled class size is, lets say, 120KB, even if I �place that class in the session� 1000+ times the memory used is only 120KB (For the class) + whatever data I have in each class?
-Chris


Yes, correct. You don't place the class in the session, but objects. The class is only loaded once and referred to by the objects, which additionally hold the instance data.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Funny.
The performance depends a lot of things: Hardware, System, VM.
If you need the age it's quicker to get it directly from SQL
<CODE>
TRUNC(MONTHS_BETWEEN(SYSDATE, T4.fechaNacimiento)/12) // ORACLE
DATEDIFF(YEAR, T4.fechaNacimiento, GETDATE()) // M$SQL
</CODE>
Your method is hem... a bit wrong. (what age I have being burn in December in January?) So you need to keep it in mind.
If you have many concurent threads it COULD be better to use LocalThread instead:
<CODE>
public final class DateValidator extends ThreadLocal
{
private static final DateValidator PARSER_THREAD = new DateValidator();

protected Object initialValue()
{
DateParser df = new DateParser();

return df;
}

public static Date parse(String year, String month, String day)
{
DateParser parser = (DateParser)PARSER_THREAD.get();

return parser.parse(year, month, day);
}

public static Date parse(String date)
{
DateParser parser = (DateParser)PARSER_THREAD.get();

return parser.parse(date);
}

public static String getLastParsedString()
{
DateParser parser = (DateParser)PARSER_THREAD.get();

return parser.getLastParsedString();
}

private static class DateParser
{
private SimpleDateFormat df;
private Date lastParsedDate;
private String lastParsedString;

private DateParser()
{
df = new SimpleDateFormat("yyyy/MM/dd");
df.setLenient(false);
}
public Date parse(String year, String month, String day)
{
return this.parse(year + "/" + month + "/" + day);
}

public Date parse(String date)
{
// obviously invalid date...
if (date == null || date.length() == 0)
{
lastParsedDate = null;
lastParsedString = null;

return null;
}

// the date is the same as just parsed...
if (date.equals(lastParsedString))
{
return lastParsedDate;
}

// workaround the SimpleDataFormat bug:
// '1990/12/2z' supposed a good date
// (even with setLenient(false) )
if (!Character.isDigit(date.charAt(date.length() - 1)))
{
lastParsedDate = null;
lastParsedString = null;

return null;
}

try
{
lastParsedDate = df.parse(date);
}
catch(ParseException ex)
{
lastParsedDate = null;
lastParsedString = null;

return null;
}

lastParsedString = date;

return lastParsedDate;
}

public String getLastParsedString()
{
return lastParsedString;
}
}
</CODE>
I know, there is a lot of junk in the code (in my case the date validation could be called twice (no by myself :-), so I had to catch it in perf. concern.
IMHO in Java (Date/Calendar/SimpleDateFormat) something is brocken... :-(
 
Chris Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dummy Jim:
Your method is hem... a bit wrong. (what age I have being burn in December in January?) So you need to keep it in mind.


That method was just for test purposes. The actual getAge() method that I have designed for this class is more complete. Thanks for input though.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic