• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

throw out-of-memory error

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
------------------------------------------
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic