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

  • SEARCH
  • Home
  • 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 8116089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:42:21+00:00 2026-06-06T03:42:21+00:00

I have a self-written Java application – a little mail monitor. It works with

  • 0

I have a self-written Java application – a little mail monitor. It works with a MySQL database that has a table which is filled somewhere else regularily. It looks through the table and sends mails as the records appear in the table.

My problem is, the application leaks memory. I was quite sure that it didn’t, as the scope of everything I use seems to disappear, making all objects used garbage collectable. But after a while (depending on the -Xmx I pass), the application stops with a OutOfHeapSpace error.

I cannot post the whole code, as it is not mine anymore, yet I tried to recreate it using pseudo code.

Main:
  Startup
  Create .lock file (FileChannel)
  Instantiate Main Class

Constructor Main:
  Class.ForName for the MySQL driver
  Read properties file (settings)
  Create connection object (MySQL)
  Fetch unsent mail ids (ArrayList)
  while(true)
    while have more mail ids
      new Thread(Top Mail ID, MySQL Connection object, Sleep Time, Blacklist);
    end while
    if have no more mail ids in ArrayList:
      sleep for a number of seconds (usually 300)
    end if
  end while

Constructor Thread:
  Prepare Statement
  New Thread(this).start();
  Sleep

Thread run():
  Select Record by passed Mail ID
  Extract everything (Sender, Receiver, Subject etc.)
  Check Blacklist, return if matched
  Extract Attachments as blobs

What I have tried so far:

jvisualvm has shown me how the memory changes over time. What I see is a jagged line in the Heap: Allocating and collecting memory happens regularily, however, after the collection there’s always a litttle more memory allocated than after the last collection. the number of Threads seem to be fine, it always drops down to the standard number.

The information amount in jvisualvm is too much for me. There are threads listed there that I cannot identify, the threads I create are not listed as my class, so it’s difficult to determine exactly what is “my” code.

Can anybody recognize any common mistakes of multithreading in my pseudo code, or recommend any tools that allow me to determine my leaks easier?

Thank you.

Edit 1: Data access via the connection object which is passed around to all threads is synchronized on itself.

Edit 2: I checked the number of mails that are unsendable (as these stay in the database), there are about 10.

Edit 3: I found Eclipse Memory Analyzer, installed it, and it hinted at the problem. It seems that using PreparedStatements while retaining one connection object keeps a HashMap of all PreparedStatements ever run over this connection inside it, therefor adding all data ever selected to it. I relied on the PreparedStatement going out of scope and being collected. I will rewrite and try if this fixes the problem.

  • 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-06-06T03:42:22+00:00Added an answer on June 6, 2026 at 3:42 am

    If this is of any interest to anybody, I fixed the memory problem.

    The application did not actually leak memory. As I passed the same connection object around, it seems that it saved the result of every prepared statement run against it in a large hashmap. Therefor, the memory usage was constantly going up. I found this out by using a heap dump created using jvisualvm and loaded into Eclipse Memory Analyzer.

    I rewrote the application to use a ThreadPool (Cached), opened a dedicated connection for every Thread and close the connection at the end of every thread.

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

Sidebar

Related Questions

I have a self-referential table in my database that looks sort of like above.
I have a self referencing table named categories that has a parentcategoryid column that
I have a self written java server that may have multiple keys (1 key
I have written a class in python that implements __str__(self) but when I use
I have Java webserver (no standard software ... self written) . Everything seems to
I have written a quick Java wrapper that fires off the NMAP executable and
I have two websites with self written membership providers that are hostet on the
I try to build a Eclipse plugin that has to use a self written
I have written a simple web application with embedded web server (tornado), database (sqlalchemy
I have a problem with the Jakarta Commons HttpClient. Before my self-written HttpServer gets

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.