Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception in thread "Timer-0" java.lang.OutOfMemoryError: Java heap space

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Exception in thread "Timer-0" java.lang.OutOfMemoryError: Java heap space

    i write a java code using time schdular in my program i get the data from databse and it will be updated into anothe databse the task is

    1. i get the data from database for every five minutes
    this program is excuted every time for every 1 minute

    my java code is:

    code=java

    public class updateremindertable {

    private Timer timer=new Timer();
    public static void main(String[] args) throws ParseException {
    // TODO Auto-generated method stub
    updateremindertable manager=new updateremindertable();
    manager.startTask();

    }
    public void data() throws Exception{
    List<Rfid1> r1;
    List<RfidDB> l;

    List<StudentInfo1> l3;
    RfidDB db;

    StudentInfo si;
    StudentInfo1 sb;
    java.util.Date d;

    List<RfidDB> l1 = new ArrayList<RfidDB>();
    List<StudentInfo> l2 = new ArrayList<StudentInfo>();
    l3 = new ArrayList<StudentInfo1>();

    sb = new StudentInfo1();
    si = new StudentInfo();
    java.util.Date d2 = new Date();
    Date d1 = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dateWithoutTime = sdf.parse(sdf.format(d1));

    Calendar c = Calendar.getInstance();
    System.out.println(c.getTime());
    c.add(c.MINUTE, -5);

    System.out.println(c.getTime());

    d2 = c.getTime();

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();

    Criteria cf = session.createCriteria(RfidDB.class).add(
    Restrictions.between("clock_in", d2, new Date()));
    l = cf.list();
    if (l.size() > 0) {
    Set s0 = new HashSet();
    List p0 = new ArrayList();
    for (RfidDB rf : l) {
    p0.add(rf.getId());
    s0.addAll(p0);
    p0.clear();
    p0.addAll(s0);

    }
    Criteria cf1 = session.createCriteria(RfidDB.class);
    Criterion one = Restrictions.ge("clock_in", dateWithoutTime);
    Criterion two = Restrictions.in("id", p0);
    Criterion complete = Restrictions.conjunction().add(one).add(two);
    cf1.add(complete);

    l1 = cf1.list();
    Set s = new HashSet();
    List pl1 = new ArrayList();
    for (RfidDB sl1 : l1) {
    pl1.add(sl1.getId());
    s.addAll(pl1);
    pl1.clear();
    pl1.addAll(s);
    }
    l2 = session.createCriteria(StudentInfo.class)
    .add(Restrictions.in("uniqueid", pl1)).list();

    List pl2 = new ArrayList();
    Set set = new HashSet();
    for (StudentInfo si1 : l2) {
    pl2.add(si1.getsNo());
    set.addAll(pl2);
    pl2.clear();
    pl2.addAll(set);

    }
    l3 = session.createCriteria(StudentInfo1.class)
    .add(Restrictions.in("sNo", pl2)).list();

    // l3 = cf2.list();
    session.close();
    sf.close();
    SessionFactory sf1 = HibernateUtil.getSessionFactory();
    Session ses = sf1.openSession();
    for (StudentInfo s2 : l2) {
    java.util.Date t = null, t1 = null;
    int count = 0;
    Rfid1 rf;
    rf = new Rfid1();
    for (StudentInfo1 s3 : l3) {
    if (s2.getsNo() == s3.getsNo()) {
    count++;
    }
    }
    int index = 0;
    for (RfidDB s1 : l1) {
    if (s2.getUniqueid().equals(s1.getId())) {
    if (index == 0) {
    t = s1.getClock_in();
    System.out.println(t);
    }
    if (index >= 1) {
    t1 = s1.getClock_in();
    }
    index++;
    }
    }

    Transaction tx1 = ses.beginTransaction();

    rf.setStatus_color("green");
    rf.setUniqueid(s2.getUniqueid());
    rf.setDate(t);
    DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
    String chdate = df.format(t);
    rf.setDate1(chdate);
    rf.setSid(s2.getsNo());
    if (t1 == null) {
    rf.setClock_in(t);
    Date currdate = new Date();
    System.out.println(currdate);

    long remtime1 = t.getTime() + (count * 2 * 60000);
    if (currdate.getTime() >= remtime1) {
    rf.setStatus_color("orange");
    }
    Date currdate1 = new Date();
    long remtime2 = t.getTime() + (count * 3 * 60000);
    if (currdate1.getTime() >= remtime2) {
    rf.setStatus_color("red");
    }
    } else {
    Date currdate = new Date();
    long remtime1 = t.getTime() + (count * 2 * 60000);
    rf.setClock_in(t);
    rf.setClock_out(t1);
    if (t1.getTime() >= remtime1) {
    rf.setStatus_color("orange");
    }
    Date currdate1 = new Date();
    long remtime2 = t.getTime() + (count * 3 * 60000);
    if (t1.getTime() >= remtime2) {
    rf.setStatus_color("red");
    }
    }
    // long diff = (long) ((( t1.getTime()-t.getTime())*1.66667

    System.out.println(rf.getClock_in());
    System.out.println(rf.getClock_out());
    ses.saveOrUpdate(rf);
    tx1.commit();

    }
    ses.close();
    sf1.close();

    }
    }
    public void startTask() {

    timer.schedule(new PeriodicTask(),0);

    }
    private class PeriodicTask extends TimerTask{


    public void run() {
    while(true){
    // TODO Auto-generated method stub
    try{
    data();
    Thread.sleep(60000);
    timer.schedule(new PeriodicTask(),60000);


    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    }



    /code
    i get the exception
    Exception in thread "Timer-0" java.lang.OutOfMemoryError: Java heap space
    at java.lang.StringCoding$StringDecoder.decode(Unknow n Source)
    at java.lang.StringCoding.decode(Unknown Source)
    at java.lang.String.<init>(Unknown Source)
    at java.lang.String.<init>(Unknown Source)
    at com.mysql.jdbc.ResultSet.getString(ResultSet.java: 1802)
    at com.mysql.jdbc.Connection.loadServerVariables(Conn ection.java:3368)
    at com.mysql.jdbc.Connection.initializePropsFromServe r(Connection.java:3199)
    at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:1788)
    at com.mysql.jdbc.Connection.<init>(Connection.java:4 40)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:400)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.hibernate.connection.DriverManagerConnectionPr ovider.getConnection(DriverManagerConnectionProvid er.java:110)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(Sc hemaUpdate.java:118)
    at org.hibernate.impl.SessionFactoryImpl.<init>(Sessi onFactoryImpl.java:252)
    at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1043)
    at com.nitspl.HibernateUtil.getSessionFactory(Hiberna teUtil.java:20)
    at com.nitspl.updateremindertable.data(updatereminder table.java:61)
    at com.nitspl.updateremindertable$PeriodicTask.run(up dateremindertable.java:198)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)



    any one tell me how resolve this problem


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exception in thread "Timer-0" java.lang.OutOfMemoryError: Java heap space

    Please don't post topics for help in Member introductions sub-forum. I requested a mod to move.

    Post your code in code tags.

    Your error is the same as one will get with infinite recursion, a method calling itself with no exit. Inspect your run() method, and:

    1. determine if the while(true) is needed (it's not);
    2. ask yourself why a new periodic task is being created;
    3. find a better way than sleeping the thread to schedule breaks in a PERIODIC TIMER.

    I recommend you refer to the Java Timer tutorial.

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 10:37 PM

Tags for this Thread