Link to home
Start Free TrialLog in
Avatar of Angus
AngusFlag for United Kingdom of Great Britain and Northern Ireland

asked on

JVM - Catch Exceptions

Hi,
I have written a Java Application using JBuilder.  Lately every now and again it is randomly stopping - when it should execute for 5 hours uninterrupted.

I then executed the application from JBuilder in debug mode to understand if any uncaught exceptions are being thrown.  I found that the exception

java.lang.OutOfMemoryError

was being thrown.  I then used Optimizeit to improve its use of memory and reduced the use from 90MB to 60MB.  The application now does not through an error on the development computer.  However when I am running it on the production server it continues to stop.

I think this is still the Out Of Memory error as the production server is executing MS SQL which is using 1.5GB of the available 2GB of memory.  The server has the following config:

Windows Prof.
1 Hard Disk partitioned into two C:/ & D:/  with a page file set on C:/ (Max 4GB)
F Drive - RAID Array with no Page File.
2 GB Physical Memory.

My questions are these:

1.  How can I catch the exception java.lang.OutOfMemoryError error as it seems to be thrown from JVM and not my code.  I would like to confirm that indeed this is the issue on the server (which does not have JBuilder)

2. How can I prevent this issue from happening.  I have a page file manually set on C:/ should I got for "System managed Size"???  

Thanks
Angus
Avatar of ashok3sep
ashok3sep

increase the heap size:

java -Xmx<size>

Regards,

Freedom
How did you optimised from 90 mb to 60 mb.........

Increasing the memory size may help us on development side, but on production server, hosting people may not give us support to increase the heap size.

So what do you need to do is

put  the code in try catch block and if you get the exception, log the values into database for which you get the exception and later execute them by some other means.

try{
  << CODE>>
}
catch(Throwable e)
{
  if(e instance of  java.lang.Throwable)
  {
            <<log the values>>
            <<clean unwanted objects>>
            << restart the method again>>  
  }
}


But note that,

1. whenever you have acieved the required functionality with particular object, then clean it/nullify the object
say something like myobject=null;

2.  don't maintain maximum variables at class level.
3.  use them at method level only. once method is finished they will go to garbage collector
4. on execution flow, if you think  particlular variable is no where used after it's usage, try to use with some other executions. This can avoid creation  of old variables

Regards
Sudhakar
 
   
Avatar of Angus

ASKER

I had been declaring a number of arrays which were not always being used (String[][] size 40,000+).  So I changed the handling of String objects, only declaring what I was using.

so for example
java -Xmx4028 would set the heap size to 4GB?

also, does this need to be set every time the computer/server starts?  How can I make it a perm change?  How can I find out the current heap size.

Also, any idea how I can catch/view JRE exceptions?

Thanks
I had done Small Mistake

It should be

try{
  << CODE>>
}
catch(Throwable e)
{
  if(e instance of  java.lang.OutOfMemoryError)
  {
            <<log the values>>
            <<clean unwanted objects>>
            << restart the method again>>  
  }
}
declaring two dimensional array with such huge sizes is poor methodology.

you should able to get them on fly.

Anyhow can you post corresponding code, so that i can help you

Regards
Sudhakar
Avatar of Angus

ASKER

thanks for your responses!  much appreciated

the issue is that there is no trace provided.  Therefore there is no specific code which I can surround in a try & catch to manage the exception.

That is why I am having such an issue with this problem - as the error could be thrown from any of the 100 of classes.

Any other suggestions?
ok,

if you know on which class you are getting that exception, then try to catch the Throwable object for that class only

Regards
Sudhakar
Ok,

one question,

Do that String array contains constant values or resultant values that are executed from the code??

try to use Hashtable in such casess

as an example
Hashtable ht=new Hashtable();
Vector vect =new Vector();
vect.add("Tiger");
vect.add("Lion");
vect.add("Monkey");
ht.put("animals",vect);

vect.add("Banana");
vect.add("Orange");
vect.add("Water Melon");
ht.put("fruits",vect);

if you do so, you can place only the objects of required size.

And note that JVM has limitations over using heap memory. So you need to concentrate further on code optimization only

Regards
Sudhakar
Avatar of Angus

ASKER

unfortunately I do not know that either.

basically when I execute the code in JBulder the error message java.lang.OutOfMemoryError appears in the execution pane. No other information is provided at all.

When I execute the application outside JBuilder, then I get no message.  The application just stops after 1.5 hours execution (approx).

if it is database related project, try to use paging concept for database communication. Don't fetch all the records at a time. On fly try to get only limited amt of data by specifying batch size. if new one is requested, clea the old list and get the new batch

Regards
Sudhakar
Why dont you release the String Array after using them .......

if you do so you can get rid of these erros.


or else explicitly call System.gc();

to call the Garbage Collector to release the Memory and i think it could be useful for your solution......


regards,

Freedom
ASKER CERTIFIED SOLUTION
Avatar of boazbl1
boazbl1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Angus

ASKER

THANKS!!!