Two Laptop Bag*
The moose likes Beginning Java and the fly likes Need help on java Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help on java Questions" Watch "Need help on java Questions" New topic
Author

Need help on java Questions

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi All,
I need your help for below set of Java Queries.Help provided will be appreciated.

1> write a Java program for USER DEFINED MARKER INTERFACE?
2> what is an Error in Java Program.How do you handle OutOfMemory Error or other relevant errors in java program Example : Assume your web application is crashing due to OutOfMemory Error, how would you handle it in your java
program.?

3> String s = new String("abc");
StringBuffer sb = new StringBuffer("def");
s = "apple";
sb ="australia";
System.out.println(" The value of String s is "+s);
System.out.println(" The value of StringBuffer sb is "+sb);

4> Write a Java program with finalize() method in it to handle garbage collection ?

--
Deepak Lal


When The Going Gets Tougher,The Tougher gets Going
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
The answers are:
1: Eli Whitney and the Cotton Gin.
2: Only when I laugh.
3: Golda Meir, and
4: Jesse Owens.

What do you think the answers are? We don't give out answers like that, as you should already know. Read these FAQs 1 2, then tell us what your answers are. Also tell us how many of those questions you think are sensible questions.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Campbell,

My Comments See below

Once again,I'm Sorry and i will make an initial effort to solve the problems on my own but still since i wasn't sure of how to proceed in a correct direction,i thought of posting it to the Java Ranch forum.

I have solved few of the above mentioned Queries on my own.

I have a clarification left.



what is an Error in Java Program.How to handle OutOfMemory Error in Java program ?

Example : My web application is crashing due to OutOfMemory Error how to handle it in your java program.?



My Java application is crashing and is showing OutOfMemoryError.Can you please guide me on how to resolve this error.

I have done an initail analysis: Using Throwable would solve the issue.so should i go with it

some thing like below



Please guide me.
Hints/Suggestions/Help/Advice provided will be highly appreciated.
I will be highly obliged if you can help me out.I have been struggling with this issue.

--
Deepak Lal
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

'OutOfMemoryError' are not necessarily a code problem - or an error that you can fix by adding more or less code - and then again - sometime it is, but in your case I think it hasn't.

You should have a look at the java VM params.
[ November 01, 2008: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Rene Larsen is right. It is worthwhile looking at the JVM parameters, but if you have set up a reasonable amount of memory, you oughtn't to get that Error. I fact I think it is a daft question; read the API documentation for Error. That suggests you ought not to handle Errors in the first place.

If a beginner's application gives an OutOfMemoryError there is almost certainly an error in the code, most likely recursive creation of objects.

And please tell us what the other answers were.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Campbell and Rene larsen,
My jsp file is containing Approximately 35000 lines of code and when i run it,i'm getting this "OutOfMemory Error",

According to Java 1.5 API:Error is a sub class of throwable that indicates serious problems that a reasonable application should not try to catch,hence we conclude that we should not catch Error in our program..
but how do i handle this Error in my Java program.


Rene Larsen,You were mentioning about JVM parameters,Can you elaborate more on that.?
how can we increase the memory so that this OutOfMemoryError can be avoided.?

Campbell, What do you mean by " Recrusive Creation of Objects that cause OutOfMemoryError" ?

Need your help still as the mystery of OutOfMemory Error remains UNRESOLVED.



--
Deepak A L
[ November 02, 2008: Message edited by: Deepak Lal ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
My jsp file is containing Approximately 35000 lines of code

That's your problem right there. Apart from the fact that JSPs should not contain any code, this many lines is a nightmare to maintain. If you refactor that page into numerous classes (as backing beans) with numerous methods each, and then use JSP/JSTL for the view only, the problem will most likely disappear without anything special being done about memory consumption.


Ping & DNS - my free Android networking tools app
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Originally posted by Deepak Lal:
Rene Larsen,You were mentioning about JVM parameters,Can you elaborate more on that.?


When you start/call your java program (or Application Server), you can set som JVM param.

On java it is possible to set the following -X JVM param, where the two in bold are interesting for you:
rlarsen:~ rlarsen$ java -X
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by :>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by :>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by :>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size

-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/VM (see documentation)
-Xdock:name=<application name>
override default application name displayed in dock
-Xdock:icon=<path to icon file>
override default icon displayed in dock
-Xcheck:jni perform additional checks for JNI functions
-Xshare ff do not attempt to use shared class data
-Xshare:auto use shared class data if possible (default)
-Xshare n require using shared class data, otherwise fail.

The -X options are non-standard and subject to change without notice.
rlarsen:~ rlarsen$


One sample could be:
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Rene and sheriff Need your help for the below thing.

As Rene Larsen mentioned i have done the below for sample Java Program





But God(Sheriff and Rene Larsen) what about the scenario in case of J2EE Web Based Application.

But what about my web Application.How to set it for a web application ??
Where do i need to set these Paramters for a Web Based J2EE Application ??



Guys,Help Provided will be highly appreciated.

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Which Application Server do you use??

and what OS do you use??

If we say you are using JBoss on Windows, you need to add the JVM param to the JAVA_OPTS variable inside "JBOSS/bin/run.bat" (on *NIX it is inside "run.conf").

Other servere has there own place from where it is started.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507


Originally posted by Rene Larsen:
Which Application Server do you use and what OS do you use??


My Comments:
Server Used is : Tomcat Server V5.5
Another Server used is : BEA Weblogic Server
Operating System : Windows XP.

Rene,Can you please tell me where should the changes be made for the VM parameters.which file needs to be changed.?
If you can tell me the exact line in the configuration file.it would be helpful.

[ November 02, 2008: Message edited by: Deepak Lal ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
And I thought all those questions were homework . . .

You really ought to tell the full details.

Question 1: If I were marking I would mark you down for the spelling errors in the interface names, but the real problem is that many people would now prefer to use annotations rather than marker interfaces.
Question 2: Recursive creation of objects is like ths situation where the creator for Foo includes "new Foo()." You will create an unlimited range of nwe objects until your memory is exhausted. Rene Larsen has already given you a suggestion about what changes to make. We don't know what's in your configuration file, so we can't help any more there.
Another possibility is that you are putting objects into Lists, Stacks, etc., and leaving them there. You need some way of setting those values to null.
Ulf has already made suggestions, which sound a lot easier than going through 35000 lines looking for unneeded references which block memory.
Question 3: You have found the error about "australia."
Question 4: It's Throwable not throwable. You ought to have closed your fileInput object in the same method it was created in (and in a finally). You ought only to use finalize() to close native resources, and I think (but may be mistaken) you ought to start with super.finalize();.
Question 5: Question 7: I only knew about 4 questions.

In fact I think all 4 questions you quoted have something wrong with them; maybe you ought not to create marker interfaces, you oughtn't to handle Errors, you can't assign a String to a StringBuilder, and you oughtn't to use finalize() for Java code.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Campbell,
I have the below clarifications...


Campbell wrote: Question 1: If I were marking I would mark you down for the spelling errors in the interface names, but the real problem is that many people would now prefer to use annotations rather than marker interfaces.

My Comments for Question 1: I'm new to annotations.How Should i proceed to write annotations instead of marker interfaces.Can you please give me a sample example with respect to Java 1.5 API.?



Campbell wrote: Question 2:Rene Larsen, has already given you a suggestion about what changes to make. We don't know what's in your configuration file, so we can't help any more there.

My Comments for Question 2: Rene mentioned about a configuration file for JBOSS Server,so i wanted to know which configuration file needs to have the VM paramters set in case of TOMCAT SERVER V5.5 and BEA Weblogic Server.?
Can you tell me what changes has to be made in the configuration file.?



Campbell wrote :Question 4: It's Throwable not throwable. You ought to have closed your fileInput object in the same method it was created in (and in a finally). You ought only to use finalize() to close native resources, and I think (but may be mistaken) you ought to start with super.finalize();.

My Comments for Question 4:sorry for making mistakes with Throwable,
I'm new to using finalize() method in java.Can you please tell me what do you mean by native resources.Can you please provide me with a sample Java example for finalize() method where you are closing native resources.?




Finally Campbell wrote:
In fact I think all 4 questions you quoted have something wrong with them; maybe you ought not to create marker interfaces, you oughtn't to handle Errors, you can't assign a String to a StringBuilder, and you oughtn't to use finalize() for Java code.

My comments for the above interpretation:
1>You ought not to create marker interfaces? --> what's wrong with this?
2>you oughtn't to handle Errors -->Strongly agree with you.go by the Error(Java 1.5 API definition)
3>you can't assign a String to a StringBuilder -- very Much true.
4>you oughtn't to use finalize() for Java code --> then why do we have finalize() method in Java?Please explain.?


Campbell,i will be highly obliged if you can clear the above concepts to me.

:roll: :roll: :roll:
--
Deepak Lal
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

In Tomcat you can add the JVM param to the JAVA_OPTS variable inside "TOMCAT/bin/catalina.bat"

WebLogic I don't know - you need to post that question in the BEA/Weblogic forum.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Campbell, Could you please clarify the questions i have posted above Rene Larsen's POST

Need your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Find a book with a chapter about annotations; Thinking In Java (Bruce Eckel) 4th edition is an example. The annotation will vary depending what you want it to represent. You may find more about tagging interfaces here.
Don't know what to do about your configuration file.
In finalize you would be using the free function call on your pointers. No, I can't give more of an example than that. Look in a book like Joshua Bloch's Effective Java. I suspect that finalize is there because it is available in C++, where it is in common use to free resources. Nice little example in C here.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Originally posted by Campbell Ritchie:
Question 1: If I were marking I would mark you down for the spelling errors in the interface names, but the real problem is that many people would now prefer to use annotations rather than marker interfaces.


And if I was marking this. I would mark you down for using annotations when I specified the use of a marker interface


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Touche, Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help on java Questions
 
Similar Threads
OutOfMemory error
OutOfMemory Error
Hibernate: OutOfMemory Error!
outofmemory reading text file
outOfMemory