Witam
mam duzy problem z optymalizacja aplikacji caly czas gdzies wycieka pamiec a ja nie jestem w stanie stwierdzic co jest problemem
http://wgrajfoto.pl/pokaz/img0610/aaadc52.jpg
http://wgrajfoto.pl/pokaz/img0610/pamiec3d05.jpg
Zrobilem program testowy by przesledzic co moze byc nie tak program wysyla 80 wiadomosci co sekunde i zaczolem dokladac metody i testowac czy pamiec nie zaczyna sie rozrastac no i problem sie pojawil jak dodalem metode ktora komunikuje sie z baza. sama metoda jest dobrze napisana bo zapetlilem ja na 100000 zapytan i pamiec nic nie nabral na objetosci. Program dobrze dziala jesli jest 1-2 klijenow uruchomiony ale jesli podlacze wiecej to zaczyna nabierac, nie mam pojecia gdzie tu szukac rozwiazania?
import java.util.Random;
import java.util.TimerTask;
public class CC_timer extends TimerTask {
public static String s=C_start.s;
public void run()
{
System.out.println("Wysylam 80 losowych wiadomosci");
for (int yy=1;yy<=80;yy++) {
Random a = new Random();
StringBuilder temp = new StringBuilder();
for (int xx=1;xx<=70;xx++) {
temp.append(a.nextInt(700));
temp.append(s);
}
C_start.wys.wyslij(temp.toString());
}
}
}
import java.io.IOException;
import java.nio.BufferUnderflowException;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
public class C_echo {
static IDataHandler clientHandler = null;
public static String s=C_start.s;
public static String k=C_start.k;
public static void Start() {
try {clientHandler = new IDataHandler() {
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
return true;
}
};
}catch(Exception e){}
}
}
public class C_send {
public static String s=C_start.s;
public static String k=C_start.k;
synchronized public void wyslij(String co) {
try {
C_start.nbc.write( "0"+s+"0"+s+"test4"+s+"Af561aaf6ef0bf14d4208bb46a4ccb3ad"+s+ co +k );
} catch (Exception ex) {}
}
}
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Timer;
import javax.swing.JApplet;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.NonBlockingConnection;
public class C_start extends JApplet {
public static String s="+"; //SEPARATOR alt 193
public static String k="T"; //SEPARATOR KONCA PAKIETU alt 194
public static Timer timer1;
public static InetAddress inetAddress=null;
public static INonBlockingConnection nbc=null;
public static C_send wys = new C_send();
public void start(){
START();
timer1 = new Timer(); CC_timer timer1_task = new CC_timer(); timer1.schedule (timer1_task, 0, 1500);
}
public static void START(){
try { inetAddress = InetAddress.getByName("82.139.61.105");
try { nbc = new NonBlockingConnection(inetAddress, 8090, C_echo.clientHandler); } catch (IOException e) {e.printStackTrace();}
System.out.println("--------------------------------------------------------");
System.out.println("| Client START! |");
System.out.println("--------------------------------------------------------");
} catch (UnknownHostException e) {e.printStackTrace();}
try {
} catch (Exception e) {e.printStackTrace();}
wys.wyslij("PROBA ZALOGOWANIA");
}
public static void STOP(){
try { if (nbc!=null) { nbc.flush(); } //else {System.out.println("STOP !flush");}
if (nbc!=null) { nbc.close(); } //else {System.out.println("STOP !close");}
} catch (IOException e) {e.printStackTrace();}
}
}
import java.sql.*;
public class SS_BAZA_mysql extends Thread {
SS_BAZA_mysql(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url+db,login, haslo);
stmt = con.createStatement();
} catch( Exception e ) {e.printStackTrace(); }
}
Connection con;
Statement stmt;
String url ="jdbc:mysql://localhost:3306/";
public String db="baza";
String login = "root";
String haslo = "haslo";
public int sprawdzamv2(String L, String H){
int flaga=0;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = "SELECT * FROM "+db+".tabelka WHERE login='"+L+"';";
//SELECT * FROM table WHERE id='$random_id'
System.out.println("query ="+query);
ps = con.prepareStatement(query);
rs = ps.executeQuery(); // po dodaniu tej lini program zaczyna nabierac duzo na pamieci
// while (rs.next()) {
// String n1 = rs.getString("login");
// String n2 = rs.getString("password");
// String n3 = rs.getString("serv");
// if (n3.equals("0")) {
//
// if (n2.equals(H) && n1.equals(L)) {System.out.println("L == H ");
// flaga=1;
// stmt.executeUpdate("UPDATE tabelka SET serv='"+1+"' WHERE login='"+L+"'; ");
// } else {System.out.println("sprawdzam: L != H ");}
//
// } else { System.out.println("N3!=0 ---> "+L+" jest na innym serwerze");}
//
// }
rs.close();
ps.close();
}catch( Exception e ) { e.printStackTrace(); }
finally {
if (rs!=null) {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
rs = null;
}
if (ps!=null) {
try {
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
ps = null;
}
}
return flaga;
}
}
import java.io.IOException;
import java.nio.BufferUnderflowException;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IDataHandler;
public class S_echo implements IDataHandler {
public static String s=S_start.s; //SEPARATOR alt 193
public static String k=S_start.k; //SEPARATOR KONCA PAKIETU alt 194
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
String wiadomosc = nbc.readStringByDelimiter(k);
System.out.println("wiadomosc = " +wiadomosc);
String stab[]=null;
stab = wiadomosc.split(s);
if (S_start.baza.sprawdzamv2(stab[2],stab[3])==1) {}
return true;
}
}
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
public class S_start {
public static String s="+"; //SEPARATOR alt 193
public static String k="T"; //SEPARATOR KONCA PAKIETU alt 194
public static SS_BAZA_mysql baza = new SS_BAZA_mysql();
public static void main(String [] args) {
try{ System.out.println("--------------------------------------------------------");
System.out.println("| Serwer ");
System.out.println("--------------------------------------------------------");
IServer srv = new Server(8090, new S_echo());
srv.run(); srv.start(); }catch(Exception e){ e.printStackTrace();}
}
}
Ten plik obrazuje ze sama funkcja działa dobrze i program nie nabiera na pamieci
import javax.swing.JApplet;
public class petla extends JApplet{
public static SS_BAZA_mysql baza = new SS_BAZA_mysql();
@Override public void start(){
System.out.println("start");
for (int xx=1;xx<=100000;xx++) {
System.out.println("xx"+xx);
if (baza.sprawdzamv2("5","5")==1) {
}
}
}
}
dolaczam kod z bibliotekami:
http://mgs_saladin.republika.pl/wyciekPamieci.zip