Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Serialization file getting bigger and bigger on the same object

Reply
Thread Tools

Serialization file getting bigger and bigger on the same object

 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      04-10-2007
In my application, I perform serialization on my JTable's TableModel.
I realize that, even I didn't make any modification (either save or
delete data) on the JTable content, however, everytime I save the
JTable's TableModel, the serialization file will getting bigger and
bigger (about 2KB)

It works this way

1. I shut down my application. My application save JTable's TableModel
to serialization file. The file size is 50 KB

2. I open up my application. My application load JTable's TableModel.
Everything in the JTable content restore.

3. I shut down my application again, without modification on the
JTable's TableModel. The file size is 52 KB.

4. If I keep repeating step 1, the file size will keep increase.

May I know how I can prevent my serialization file from getting bigger
and bigger?

Thanks


--------code----------
/*
* NewJFrame.java
*
* Created on April 8, 2007, 11:40 PM
*/

import java.io.*;
import javax.swing.table.*;

/**
*
* @author doraemon
*/
public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();

loadTableModel();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code
">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();


setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
});

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(25, Short.MAX_VALUE))
);
pack();
}// </editor-fold>

private void formWindowClosed(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
saveTableModel();
System.out.println("table model saved");
}

private boolean saveTableModel() {
try {
FileOutputStream fos = new
FileOutputStream("table.ser");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(jTable1.getModel());
out.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}

return true;
}

private boolean loadTableModel() {
TableModel tableModel = null;

try {
FileInputStream fos = new FileInputStream("table.ser");
ObjectInputStream in = new ObjectInputStream(fos);
tableModel = (TableModel)in.readObject();
in.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}
catch(ClassNotFoundException exp) {
exp.printStackTrace();
return false;
}

if(tableModel != null)
jTable1.setModel(tableModel);

return true;
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration

}

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      04-10-2007
wrote:
>In my application, I perform serialization on my JTable's TableModel.


I ran your program as you posted it, and noted
the same effect you report, file size increasing
from an initial 8Kb, to 9.5, then 11.

Looking in the ser file, I noted a lot of cruft gets
serialised as well, so decided to try a run just
serializing the data.*

The size dropped to around 290-310 bytes (including
4 smallish values in the table data), but was still a little
'indefinite'.

Why not use XMLEncoder/Decoder instead?
The advantages are twofold.
- Serialization might well fail between class changes
(read as J2SE upgrades) - it is fragile.
- The XMLEncoder is optimized to store only what is
necessary, and will probably lead to a more predictable
file size.

* In any case, here is my variant that focuses on the
table data itself..

<sscce>
/*
* NewJFrame.java
*
* Created on April 8, 2007, 11:40 PM
*/

import java.io.*;
import javax.swing.table.*;
import java.util.Vector;

public class NewJFrame extends javax.swing.JFrame {

Vector columnIdents;

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();

loadTableModel();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
columnIdents = new Vector();
for (int ii=1; ii<5; ii++) {
columnIdents.addElement(
new String("Title " + (ii)));
}
Vector data = new Vector();
for (int ii=1; ii<5; ii++) {
data.add( new Vector(4) );
}

setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
});

jTable1.setModel(new javax.swing.table.DefaultTableModel(
data, columnIdents
));
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(25, Short.MAX_VALUE))
);
pack();
}// </editor-fold>

private void formWindowClosed(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
saveTableModel();
System.out.println("table model saved");
}

private boolean saveTableModel() {
try {
FileOutputStream fos = new
FileOutputStream("table.ser", false);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(((DefaultTableModel)jTable1.
getModel()).getDataVector());
out.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}

return true;
}

private boolean loadTableModel() {
//TableModel tableModel = null;
Vector data = null;

try {
FileInputStream fos = new FileInputStream("table.ser");
ObjectInputStream in = new ObjectInputStream(fos);
//tableModel = (TableModel)in.readObject();
data = (Vector)in.readObject();
in.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}
catch(ClassNotFoundException exp) {
exp.printStackTrace();
return false;
}

//if(tableModel != null)
if(data != null) {
((DefaultTableModel)jTable1.getModel()).
setDataVector(data, columnIdents);
}

return true;
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration

}
</sscce>

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200704/1

 
Reply With Quote
 
 
 
 
Filip Larsen
Guest
Posts: n/a
 
      04-10-2007
wrote:

> In my application, I perform serialization on my JTable's TableModel.
> I realize that, even I didn't make any modification (either save or
> delete data) on the JTable content, however, everytime I save the
> JTable's TableModel, the serialization file will getting bigger and
> bigger (about 2KB)


DefaultTableModel extends AbstractTableModel and this class serializes
all listeners that implement Serializable. Since JTable register itself
as a listener with its table model, you are actually serializing the
whole JTable instance along with the table model. When data is
deserialized back in, the old JTable instance will just "sit" inactive
in the listener list of the table model and next time you serialize the
table model you will now get 2 JTable instances written out. And so on.

The fittest work-around seems to be what Andrew did, namely to serialize
only the vector data of the table model.


Regards,
--
Filip Larsen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to move from java object serialization to xml serialization? Dimitri Ognibene Java 4 09-02-2006 07:32 AM
Object serialization XML vs java serialization plasticfloor@gmail.com Java 3 06-14-2006 03:45 AM
Serialization Problems and books on serialization? sinleeh@hotmail.com Java 8 01-02-2005 02:40 PM
avoiding XML serialization, different WSDL generation, soap serialization Ramunas Urbonas ASP .Net Web Services 1 07-27-2004 09:57 PM
App getting bigger and bigger Yannick Turgeon Perl Misc 1 10-14-2003 04:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57