A mehod should be Syncronized only when any shared objects are passed to
it.
If you are passing a reference to an object which is not shared across
pages, then there is no need of making your methods syncronized. Using
Synronized methods slows down a program..
Looking at your function, I am not quite sure what are you fetching from
the HaspMap.
Also , as suggested by richard..avoid using new String("..."). They take
up a lot of memory.
To be more clear about the code you write..read "Tuning Java
Performance" . You can find all kinds of performance tips in there.
Regards
Purav
-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]] On Behalf Of
Anantha_Krishnan
Sent: Friday, August 24, 2001 12:27 PM
To: [EMAIL PROTECTED]
Subject: Re: Out Of Memory Space Error....StringBuffer.append problem
thanks richard
You visualized the whole scenario by just glancing a function. SIMPLY
GREAT Yes u r write I was actually seeing the values of the variable
parameterOutput using the SOP and just commented it when it was no
longer need. Ive taken ur comment especially the design issue into my
mind and lemme think abt it. Ur comments were really good ones. Yes as u
said I was passing the HttpServlet response or request object itself to
invoking programs to decide upon whether the GET or POST method of the
servlet needs to be invoked. I understood ur synchronized explanation.
Can u just throw some more light with simple example if u have time.
bCos
a) Iam using hashmap across the site which i think becomes heavy and
calls for some re-thinking
b) Iam using Class.forName("the java class") and obj.getInstance() to
get the instance of the business object. All over here i have used
synchronized to make sure the values obtained across the pages are
consistent. But now i have to think abt 350 programs like this in the
site for removing the httpservlet instance object being passed across
the site.:). Also occassionally i receive an error saying that "The
business object is not instantiated-->account.GET". Upon refreshing its
come up. Pretty strange.
regards
ananth
-----Original Message-----
From: Richard Yee [mailto:[EMAIL PROTECTED]]
Sent: Friday, August 24, 2001 2:28 PM
To: [EMAIL PROTECTED]
Subject: Re: Out Of Memory Space Error....StringBuffer.append problem
Ananth,
A couple of comments on your code:
1) I am almost certain that the method should not be synchronized.
There aren't any variables or data structures that are being used that
are shared.
2) It is a bad practice to have a data structure like HttpServletRequest
as a parameter to a domain object especially when the only value used
from the data structure is the value of the String returned by the
req.getParameter("TOWHERE") method call. This practice is called
"Exposing Presentation-Tier Data Structures to Domain Objects" from pg.
51 of Core J2EE Patterns) The reason this is a bad practice is that it
needlessly couples two distinct aspects of the application. It also
limits the ability for the function to be reused b/c it makes the
function protocol or tier-specific. In this case, the req variable
could just be replaced by a String variable called toWhere or another
meaningful name.
3) The counter variable is being used just as a flag and should
therefore be replaced with a boolean.
4) There are several instances where you use 'new
String("displayHeader")' . I think all of these can be replaced with
just "displayHeader".
5) The getOrPost int parameter should just be a boolean since it only
has two values. Constants.HTTP_GET or Constants.HTTP_POST. A more
meaningful name for the variable would be isPost or isGet. It would
make the code more understandable
6) The parametersOutput variable seems to be unused unless there was
additional code that you cut. It seems to me that this variable is
getting appended to itself repeatedly. You might want to put a
System.out.println("parametersOutput = " + parametersOutput); in your
code to see what the value is.
7) The entire function can be distilled to the following:
>private void fillSetHeaderProperty(HashMap map, String toWhere,boolean
isGet)
{
int i = 0;
toWhere = toWhere.trim();
if (isGet)
toWhere = toWhere + ".GET";
else
toWhere = toWhere + ".POST";
while (i<Constants.RESTRICT_HEADER_DISPLAY.length)
{
if
(toWhere.equals(Constants.RESTRICT_HEADER_DISPLAY[i].trim()))
{
if (isGET)
> map.put("displayHeader","N");
else
map.put("displayHeader","Y");
break;
}
i++;
}
if (i == Constants.RESTRICT_HEADER_DISPLAY.length)
map.put("displayHeader","Y");
}
At 10:20 AM 8/24/01 +0800, you wrote:
>I have zeroed upon a simple function to depict the scenario Iam facing.
>There are multiple other functions like this..
>
>This function fills the header part of the page with parameters fetched
from
>the HashMap and also depending upon the get or post method called by
>the invoker servlet.
>
>
>////////////////////Function Begin
>
> private synchronized void fillSetHeaderProperty(HashMap map,
>HttpServletRequest req,int getOrPost)
> {
>
> String tempStr=null;
> String toWhere=req.getParameter("TOWHERE");
> String tempStr1 = null;
>
> int counter=0;
>
> for(int i=0; i <
> Constants.RESTRICT_HEADER_DISPLAY.length;
>i++)
> {
> tempStr =
> Constants.RESTRICT_HEADER_DISPLAY[i];
>
> if(getOrPost==Constants.HTTP_GET)
> {
> tempStr1 = toWhere + ".GET";
> if
>((tempStr1.trim()).equals(tempStr.trim()))
> {
> map.put(new
String("displayHeader"),
>"N");
>
>///////////////////////////////////////////////////////////////////////
>////
/
>/////////////////////
>
>//COMMENTED BY ANANTH THE PARAMETERVARIABLE I SUSPECT AND I
COMMENTED
>FROM HERE
>
>
>
>//parametersOutput = parametersOutput + "<br><b>Name:</b> "
>+
new
>String("displayHeader") + " <b>Value:</b> N" ;
>
>
>
>/////////////////////////TILL HERE
>
>
>
> counter =counter + 1;
> }
> }
> else
> {
> tempStr1 = toWhere + ".POST";
>
> if
>((tempStr1.trim()).equals(tempStr.trim()))
> {
> map.put(new
String("displayHeader"),
>"N");
>
>
>
>//COMMENTED BY ANANTH SIMILARLY HERE ALSO
>// parametersOutput =
parametersOutput
>+ "<br><b>Name:</b> " + new String("displayHeader") +
>" <b>Value:</b> N" ;
>
>
> counter=counter+1;
> }
> }
> }
> if (counter==0)
> {
> map.put(new String("displayHeader"), "Y");
>
>
>//COMMENTED BY ANANTH
>// parametersOutput = parametersOutput + "<br><b>"
+
>tempStr + "</b><br><b>" + tempStr1 + "</b><b>Name:</b> " +
>new
>String("displayHeader") + " <b>Value:</b> Y" ;
>
> }
> }
>
>
>
>
>
>
>/////////////////Function Ends
>
>
>Suggest me whether this is the "solution" or anyother is there. So far
>i
did
>not receive the error and the site runs pretty fast.This variable
>"parametersOutput" i was using to view the parameters of the pages I
>left
it
>just by commenting the out.println. But the variable was getting
>populated again and again and may be it threw that error. Correct me
>whether the solution is right.
>
>thanks
>ananth
>
>_______________________________________________________________________
>____
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
________________________________________________________________________
___
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
________________________________________________________________________
___
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html