Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

throw out-of-memory error  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am testing some code. But when it writes 20000 object out into disk, it will throw out-of-memory error. How can I correct ?

Thanks

-------------------------------
import java.io.*;
import java.util.*;
import javax.swing.*;
class TestObjectTrans implements Serializable {

private int value;
private transient String name;
private DatetimeStamp;
private transient JPanel panel;

public TestObjectTrans(int value) {
this.value = value;
timeStamp = new Date();
initTransients();
}

public void initTransients() {
name = new String("Object:" + value);
panel = new JPanel();
panel.add(new JTextField());
panel.add(new JButton("Help"));
panel.add(new JLabel("This is a text label"));
}

private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
initTransients();
}
}

/**
* The StopWatch class represents a very simple stop watch
* that can be used to time events.
*/
class StopWatch {

private long startTime;
private long stopTime;

/**
* Construct a new StopWatch object.
*/
public StopWatch() {
}

/**
* Start this StopWatch object. Starting a StopWatch
* that was already running resets the watch.
*/
public void start() {
startTime = System.currentTimeMillis();
}

/**
* Stop this StopWatch object.
*/
public void stop() {
stopTime = System.currentTimeMillis();
}

/**
* Get the time recorded by this StopWatch object.
* The value returned if getTime is called before this
* StopWatch has been stopped is meaningless.
*/
public long getElapsedTime() {
return stopTime - startTime;
}
}


public class Test {
Vector vector = new Vector();
public Test(){ }

public void writeOut(){
for (int i =0;i <10000; i++) {
vector.addElement(new TestObjectTrans(i));
}
StopWatch timer = new StopWatch();
timer.start();
try {
OutputStream file = new FileOutputStream("Out.test");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(vector);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
timer.stop();
System.out.println("write out elapsed = " + timer.getElapsedTime());

}

public void getIn(){
StopWatch timer = new StopWatch();
timer.start();
try {
InputStream file = new FileInputStream("Out.test");
InputStream buffer = new BufferedInputStream(file);
ObjectInputStream in = new ObjectInputStream(buffer);
vector = (Vector)in.readObject();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
timer.stop();
System.out.println("get in elapsed = " + timer.getElapsedTime());

}

public static void main(String[] aa){
//new Test().writeOut();
new Test().getIn();
}
}
------------------------------------------
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating 20000 of anything is going to take a huge chunk of memory. If you don't want to run out of memory don't store all 20k instances in memory. Create one (or a reasonable subset: 10,100) and write it (them) to disk.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!