Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Question on using a background thread with java.lang.OutOfMemoryError

800295Jan 13 2010 — edited Jan 27 2010
Below is the java file which will do the background job.
package apps.background;

import apps.AppIFTSTA;
import apps.gui.GuiIFTSTA;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JTextPane;
import policy.FilePolicy;

public class BackgroundIFTSTA2 implements Runnable {

    private static final long MINUTES = 60000L;
    private static final long HOURS = 60000L * MINUTES;
    private static final long TASKINTERVAL = 1 * MINUTES;
    private JButton callerJButton = null;
    private JTextPane callerJTextPane = null;
    private int dateRange = 0;
    private long SCHEDULEINTERVAL = 0L;
    private static Logger logger = null;


    public BackgroundIFTSTA2(JButton argJButton, JTextPane argJTextPane, int argDateRange, int argScheduleInterval) {
       callerJButton = argJButton;
        callerJTextPane = argJTextPane;
        dateRange = argDateRange;
        SCHEDULEINTERVAL = argScheduleInterval * HOURS;
    }

    protected void publish(String val) {
        callerJTextPane.setText(callerJTextPane.getText() + val + FilePolicy.lineSeparator);
    }

    @Override
    @SuppressWarnings("static-access")
    public void run() {
        while(!callerJButton.getText().equalsIgnoreCase("START")) {
            try {
                addMessage("Processing......");
                AppIFTSTA.process(dateRange);
                Thread.currentThread().sleep(45000L);
                addMessage("Completed......");
                callerJButton.setEnabled(true);
                Thread.currentThread().sleep(30000L);
                clearMessage();
                callerJButton.setEnabled(false);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    private void clearMessage() {
        callerJTextPane.setText(null);
    }

    private void addMessage(String msg) {
        callerJTextPane.setText(callerJTextPane.getText() + msg + FilePolicy.lineSeparator);
    }

}
And below is the segment of the file to start a new thread of execution for the background job, I only pasted segments here because the code runs fine and the problem is not here.
...
private void processBackground() {
        BackgroundIFTSTA2 runnable = new BackgroundIFTSTA2(startButton, messageTextPane, Integer.parseInt(dayUnitField.getText()), Integer.parseInt(scheduleUnitField.getText()));
        thread = new Thread(runnable);
        thread.start();
}
...
I debug my application with the following arguments:
-Xms64m -Xmx128m
The result of the above setting is the program will throw this exception and hanged after ~20 minutes.
Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space
        at org.hibernate.mapping.Column.getAlias(Column.java:93)
        at org.hibernate.mapping.Column.getAlias(Column.java:119)
        at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:529)
        at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
        at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
        at controller.MdlIndexJpaController.<init>(MdlIndexJpaController.java:20)
        at process.GenIFTSTAProcess.process(GenIFTSTAProcess.java:39)
        at apps.AppIFTSTA.normalTest(AppIFTSTA.java:52)
        at apps.AppIFTSTA.process(AppIFTSTA.java:70)
        at apps.background.BackgroundIFTSTA2.run(BackgroundIFTSTA2.java:43)
        at java.lang.Thread.run(Thread.java:619)
Jan 13, 2010 6:52:47 PM org.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:sqlserver://xxx.xx.xxx.xx;databaseName=mydataBase
which is my background thread.

-Xms64m -Xmx1024m
It apparently looks fine as the memory is 4 times larger, however, when I checked the window task manager, it seems that the application keeps on increasing the consumption of memory, and I doubted if it will soon be terminated just like the first setting.

Hence, my main question is, my program just start one thread of execution only from the main thread, and is there any way for me to "free" out the memory for JVM to consume? Please help and suggest if any, thanks.

Edited by: roamer on 2010?1?13? ??6:53
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2010
Added on Jan 13 2010
26 comments
638 views