I'm doing a copy paste, from excel to JTable
. The problem is that it consumes too much memory. I have looked for solutions to do that procedure in other ways but I can not find it. If anyone knows another way to do it, I appreciate it because I've been looking for everything for 1 month and trying everything.
The code is as follows:
public void pegar() {
Runnable miRunnable = new Runnable() {
@Override
public void run() {
try {
Clipboard clp = Toolkit.getDefaultToolkit().getSystemClipboard();
String total = (String) (clp.getContents(this).getTransferData(DataFlavor.stringFlavor));
clp=null;
StringTokenizer stk1 = new StringTokenizer(total, "\n");
//dejo total en null
total = null;
System.gc();
dtm.setRowCount(stk1.countTokens());
int max = 0;
jProgressBar1.setMaximum(stk1.countTokens());
ArrayList<String>lista = new ArrayList<>();
for (int i = 0; stk1.hasMoreTokens(); i++) {
String valor = stk1.nextToken();
for (int j = 0; j < 36; j++) {
valor = valor.replaceAll("\t\t", "\t \t");
}
lista.add(valor);
}
//dejo el clipboard en null para liberar memoria
stk1 = null;
System.gc();
int size = lista.size();
// System.out.println(lista.);
for (int i = 0; i < size; i++) {
StringTokenizer stk2 = new StringTokenizer(lista.get(0), "\t");
for (int j = 0; stk2.hasMoreElements(); j++) {
String value = stk2.nextToken();
jTable1.setValueAt(value, i, j);
}
lista.remove(0);
max++;
System.out.println(max);
jProgressBar1.setValue(max);
}
lista = null;
Runtime runtime = Runtime.getRuntime();
System.out.println("");
jProgressBar1.setForeground(new Color(51,0,102));
System.out.println("me voy a poner en pausa");
Thread.sleep(3000);
System.out.println("voy a limpiar");
runtime.gc();
} catch (UnsupportedFlavorException|IOException|InterruptedException ex) {
}
}
};
System.gc();
hilo = new Thread (miRunnable);
hilo.start();
}
I put several gc, but they did not work, I read in SO that the gc does not help much, more or less I'm hitting 5000 rows with 25 columns and this is what happens when I hit it:
The heap never decreases, it keeps using up to 1gb and I need to leave all that memory in use, it's too late beating, I do not know what to do. If you have any reference or any way to do it, I would be very grateful, continuing in the profile, if I press the button to collect garbage in netbeans this immediately decreases the memory:
Here is an image of the Excel from which I extract the information to be copied, this file has 4500 rows and 33 columns.
And this is the jtable where I hit it.
PS: I'm new to java, all this I did reading and watching videos.