Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:21:56+00:00 2026-05-26T22:21:56+00:00

* EDIT: I found the answer to the memory leak myself, and posted it

  • 0

*EDIT: I found the answer to the memory leak myself, and posted it along with the others. If someone can answer why I have to do apply the fix at all (see question in my answer), I will gladly award the answer 🙂 *

I wrote a simple util that periodically reads log files and saves the entries to a database. Not much code and seemingly works fine. But after running it on a big lump of logs today I found that it was not production ready … After the first job was finished chewing through all of the logs, the next 15 jobs would fire off, until no more connections could be established and the next job would fail with a SqlException. For the next five hours the app spewed out exceptions unnoticed until running out of memory.

The job was set up using the following cron trigger, “* */2 * * * ?”, which means “run every two minutes”. The weird thing is that after the first job was finished (which took approx. an hour) the remaining jobs would fire rapidly, one after another. This was unexpected, as I would think there had to be two minutes between each job.

Now I wonder what could be wrong. I am guessing the reason is either in how I am using Hibernate and/or Quartz. Maybe I am not releasing the session as I should, or have misunderstood how Quartz schedules jobs?Or maybe I should inject the Hibernate factory into to the job, and not create it in each job? getCurrentSession() vs openSession()? No idea. I still do not understand why this should have any effect, as the jobs should release all resources on exit. Anyway, most of the relevant code is to be seen below.

No matter if I get the connection problem sorted, the code is suffering from some sort of memory leak. It is as if every job executed is somehow kept on the heap. By executing

java -Xmx17M -DcronTriggerExpression="*/1 * * * * ?" -jar myjar.jar,

I can force the problem to appear by running the program with a minimal amount of memory. It just takes four consecutive runs in this case before I get a OutOfMemoryError when building the session factory for hibernate.

public class ExportJob implements Job {
    LogImporter importer;

    public ExportJob() { //needed for Quartz 
        setUpHibernate(); 
        importer = new LogImporter(); //injected, but this saves space on SO :)
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        /* even if this body is empty, I get a OutOfMemoryError */        
    }

    private SessionFactory setUpHibernate() {
        logger.debug("Setting up hibernate");
        this.sessionFactory = new Configuration().configure().buildSessionFactory();
        return this.sessionFactory;
    }

    private Session getSession() {
        return sessionFactory.openSession();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:21:56+00:00Added an answer on May 26, 2026 at 10:21 pm

    At least the rapid firing is caused by Quartz. It’s queuing up jobs every 2 minutes, so after one hour you should have 59 jobs itching to start.

    You are right, closing the SessionFactory, not the session fixes the problem. That also explains running our of Connections, just to close the loop – and hopefully get an award ;-). Although Hibernate allows multiple SessionFactory, each one will maintain a Connection.

    From their documentation:

    When all mappings have been parsed by the
    org.hibernate.cfg.Configuration, the application must obtain a factory
    for org.hibernate.Session instances. This factory is intended to be
    shared by all application threads. Hibernate does allow your
    application to instantiate more than one org.hibernate.SessionFactory.
    This is useful if you are using more than one database.

    So the final fix should be to create one SessionFactory elsewhere and reuse it to generate sessions in each thread, or job in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I actually found the answer. I can't close the question as I am
EDIT: I found out that I can get it to compile if I cast
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
EDIT: The entire code and database creation script can be found from http://gitorious.org/scheator .
I've found that on some occasions I can edit the source while debugging. Are
I have been told to edit this file in Sharepoint Designer: /_layouts/KWizCom_WikiPlus/CreateNew.aspx I found
Here is a hum-dinger of a problem that I have not found an answer
EDIT: I found the answer here. Returning the String found with a regular expression
[edit] Found the solution. Reinstall EVERYTHING - xcode, mono, monodevelop and monotouch. Now it
EDIT 9-3-10: I found this blog entry recently that was very enlightening. http://optimizermagic.blogspot.com/2007/12/outerjoins-in-oracle.html There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.