Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Problems with Windows Memory executing java program

Thread view: 
Sal - 16 Oct 2007 15:24 GMT
Hi all,
I've some problems with Windows memory and a java program.
I've developed a program that takes some data from serial port and put
it in a Mysql database.
Every 20 sec there are some new data in the serial port and the
program runs 24h.

I start the program in this way:
java -Xms32m -Xmx512m -jar application.jar

but the windows memory in the task manager after some hours is too
hight (90Mb) and after some days the program is freezed.
I've tried to call Garbace collector many times in my code:
System.gc();
System.runFinalization();
System.gc();

But it seems that doesn't work...
I have a massive use of select in the Mysql Database (20 select every
4 minutes) and after i use System.gc();...

But the memory always grows up!

thanks to all

Best Regard

Sal
Sal - 16 Oct 2007 16:20 GMT
The class that I use for execute a query on a mysql database is:
<CODE>
package tk_data_3_0b;

import java.sql.*;

public class ExecuteQueryDB {

    String Val_min;
    String Val_max;
    String S_max;

    String param1;
    String param2;
    String param3;
    String param4;
    String param5;
    String param6;
    String param7;
    String param8;
    String param9;
    String param10;
    String param11;
    String param12;
    String param13;
    String param14;

    ResultSet resultset;

    public ExecuteQueryDB (String sql, int nparam){
    String DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/db";

    try {
     Class.forName(DRIVER);

     Connection connection = null;
      try {
     connection = DriverManager.getConnection( DB_URL, "user", "pw");

     Statement statement = connection.createStatement();

     resultset = statement.executeQuery(sql);

     while (resultset.next()) {
         switch (nparam){
         case 1:{
             param1 = resultset.getString(1);
             break;
         }
         case 2:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             break;
         }
         case 3:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             break;
         }
         case 4:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             break;
         }
         case 5:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             break;
         }
         case 6:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             break;
         }
         case 7:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             break;
         }
         case 8:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             break;
         }
         case 9:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             param9 = resultset.getString(9);
             break;
         }
         case 10:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             param9 = resultset.getString(9);
             param10 = resultset.getString(10);
             break;
         }
         case 11:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             param9 = resultset.getString(9);
             param10 = resultset.getString(10);
             param11 = resultset.getString(11);
             break;
         }
         case 12:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             param9 = resultset.getString(9);
             param10 = resultset.getString(10);
             param11 = resultset.getString(11);
             param12 = resultset.getString(12);
             break;
         }
         case 13:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             param9 = resultset.getString(9);
             param10 = resultset.getString(10);
             param11 = resultset.getString(11);
             param12 = resultset.getString(12);
             param13= resultset.getString(13);
             break;
         }
         case 14:{
             param1 = resultset.getString(1);
             param2 = resultset.getString(2);
             param3 = resultset.getString(3);
             param4 = resultset.getString(4);
             param5 = resultset.getString(5);
             param6 = resultset.getString(6);
             param7 = resultset.getString(7);
             param8 = resultset.getString(8);
             param9 = resultset.getString(9);
             param10 = resultset.getString(10);
             param11 = resultset.getString(11);
             param12 = resultset.getString(12);
             param13= resultset.getString(13);
             param14 = resultset.getString(14);
             break;

         }

         }

    }
    } catch (SQLException e) {

        e.toString() ;
        System.out.println(e);
     } finally {
         try{

                 connection.close();
         }catch (SQLException e) {e.toString();
         System.out.println(e);}
       }
    }catch (ClassNotFoundException e) {
        e.toString();
        System.out.println(e);}
}

}
</CODE>

and i call this class with:
<CODE>
ExecuteQueryDB exqimp =new ExecuteQueryDB("SELECT column FROM table)",
1);
</CODE>

The problem can be here?

Thanks

Sal
RedGrittyBrick - 16 Oct 2007 17:30 GMT
> The class that I use for execute a query on a mysql database is:
> <CODE>
[quoted text clipped - 22 lines]
>     String param13;
>     String param14;

I'd replace the above with
         static final int MAXPARAMS = 14;
         String[] param = new String[MAXPARAMS];

>     ResultSet resultset;
>
>     public ExecuteQueryDB (String sql, int nparam){

        if (nparam > MAXPARAMS)
             throw new IllegalArgumentException(
                     "Max " + NPARAMS + " params!");

>     String DRIVER = "com.mysql.jdbc.Driver";
>     String DB_URL = "jdbc:mysql://localhost:3306/db";
[quoted text clipped - 160 lines]
>
>          }

I'd replace that 100+ line humungous switch statement with
        // note: array indexed from 0, resultset from 1
        for (int i = 0; i<nparam; i++) {
            param[i] = resultset.getString(i+1)
        }

>          }
>
>     }
>     } catch (SQLException e) {
>
>         e.toString() ;

    e.toString() does nothing useful! Try removing it.

>         System.out.println(e);
>      } finally {
[quoted text clipped - 19 lines]
>
> The problem can be here?

I'd guess elsewhere. If you can trim it down to an SSCCE* that shows the
problem, I'm sure more people will make the effort to help you.

*SSCCE defintions:
http://homepage1.nifty.com/algafield/sscce.html
http://mindprod.com/jgloss/sscce.html
Sal - 16 Oct 2007 18:53 GMT
[..]
> I'd guess elsewhere. If you can trim it down to an SSCCE* that shows the
> problem, I'm sure more people will make the effort to help you.

Thanks Red,
for your time and your advices.
I've noted that i dont close the statement and the resultset in that
class.
useful page:
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.
udb.doc/ad/cjvjdbas.htm


now i try to write a SSCCE but is a bit complicated because the
original structure is:
graphical interface - every 20 sec write and read from serial port-
than look in to a string and put some data in a DB

I could create a simulation of this process but i dont know if it is
useful for my memory problem.

Anyway i try to do it

thanks a lot

Sal
Andrew Thompson - 17 Oct 2007 02:45 GMT
...
> I could create a simulation of this process but i dont know if it is
> useful for my memory problem.

If you can create a short, self contained* simulation
and post it here, people can help you better.
If, OTOH, you can create a short example that does
*not* suffer the memory leak, the answer is 'simple'.
Just remove whatever it is in the live code that is
not in the short example, one line at a time, until the
problem is solved.  The problem will be that last
line(s) removed.

* Obviously it cannot be entirely self contained
unless you have a DB available for free interrogation
from 'dudes off usenet' (which, no - don't do that),
but even short of the DB, the self contained,
failing code would be valuable.

> Anyway i try to do it

That's good strategy.

As an aside.  The word 'I' should always be
Upper Case.

Andrew T.
Sal - 17 Oct 2007 08:49 GMT
I would like to use a memory profiler, do you have any suggestion?
I've found many program (also try and buy) but which is the best, less
complicated, useful, etc...

Thanks

Sal
Sal - 19 Oct 2007 18:13 GMT
Maybe i've founded the problem!
If I minimize the graphical interface and then i maximize the memory
go down from 120 Mb to 5Mb!

Can you help me?
Can i minimize and maximize the application every day?

thanks

Sal
Patricia Shanahan - 19 Oct 2007 18:24 GMT
> Hi all,
> I've some problems with Windows memory and a java program.
[quoted text clipped - 8 lines]
> but the windows memory in the task manager after some hours is too
> hight (90Mb) and after some days the program is freezed.

You appear to have told java to use up to 512 MB for the heap, but
consider 90 MB to be too high. Have you tried changing the -Xmx value to
a heap size you would consider acceptable?

Patricia
Sal - 22 Oct 2007 15:36 GMT
[...]
> > java -Xms32m -Xmx512m -jar application.jar
>
[quoted text clipped - 6 lines]
>
> Patricia

Yes when the memory increazed too much i change the Xmx value to 512m,
could i have any problem?

To minimize and to maximize the graphical windows i do:
<CODE>
Myframe.setState( Frame.ICONIFIED );
Myframe.setState( Frame.NORMAL );
</CODE>
and i've resolved the problem!
But i dont understand where memory leaks are?
:)
I try in next post to write a SSCCE* o my graphical interface.

Best Regards

Sal

*SSCCE defintions:
http://homepage1.nifty.com/algafield/sscce.html
http://mindprod.com/jgloss/sscce.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.