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 8137923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:19:55+00:00 2026-06-06T11:19:55+00:00

I have coded a background logging thread for my program, if a class needs

  • 0

I have coded a background logging thread for my program, if a class needs a logger it pulls it from my threadpool, so for each filename there is only one log running. The class, adds anything which needs to be logged via log(String).

Anyway whenever I set logging on and it runs the writetolog() after a while I get heapoutofmemory exception. This is caused by the log threads, but I can’t see where the memory leak is, and I am not that great at threading. My only idea is that it is in the buffered writer?

import java.io.File;
import java.io.IOException;

import java.io.FileWriter;
import java.util.Calendar;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Log extends Thread{
private String file;
private BlockingQueue<String> pq = new LinkedBlockingQueue<String>();
private BufferedWriter bw;
private boolean Writing;

@Depreciated
public Log(){
    super();
    file = "log.txt";

    start(); 
}

public Log(ThreadGroup tg, String fileName){
    super(tg,fileName);
    file = fileName;
    try {
        new File(file).createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    start(); 
}

public Log(String fileName){
    file = fileName;
    try {
        new File(file).createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    start(); 
}

@Override
public void run(){
    //System.out.println("Log Thread booted " +file);

    while(Run.running){
        if (!Writing){
            if(Run.logging)
            writeToLog();
        }
        try{
            Thread.sleep(500);
        }catch(InterruptedException e){
            Thread.currentThread().interrupt();
            break;
        }


    }
    //System.out.println("Log Thread shutting down " +file);
}

public synchronized void log(String s){
    if(Run.logging)
    pq.add(s);
}

private void writeToLog(){
    try{
        Writing = true;

        bw = new BufferedWriter(new FileWriter(file, true));
    while(!pq.isEmpty()){

            bw.write(Calendar.getInstance().getTime().toString() +" " +pq.poll());
            bw.newLine();

    }

    bw.flush();
    bw.close();
    Writing = false;
    }catch(Exception e){Writing = false; e.printStackTrace();}
}



}

EDIT – It is worth mentioning as well that in the context of the program it is logging 100’s – 1000’s of lines

Many thanks
Sam

  • 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-06T11:20:01+00:00Added an answer on June 6, 2026 at 11:20 am

    If your background thread doesn’t write to the disk fast enough, the LinkedBlockingQueue (whose capacity you left unspecified) will grow until it contains Integer.MAX_VALUE strings. That’s too much for your java heap size.

    Specify a capacity so that, in case of a full queue, the thread calling the log method will wait while some part of the queued log is dumped on disk :

    private BlockingQueue<String> pq = new LinkedBlockingQueue<String>(1000);
    

    Use put instead of add in the log method so that the logging operation waits instead of throwing an exception.

    (did you notice that you write the time at writing on disk instead of the time at logging ?)

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

Sidebar

Related Questions

First some background. I have a batch-type java process run from a DOS batch
I have a coded a program in C for linux. At the moment I
Edit 4: A new formatting for the question BACKGROUND: I have a class, Window,
Here is some jQuery I have coded: $(function () { $('input').click(function() { $(this).css(background, -webkit-gradient(linear,left
I have one main thread, and many other background threads. The main usage of
I have some image processing code that runs on a background thread and updates
I am from Database Development / QA background. I have not exhaustively worked on
i need to set image as background in android gridview. I have code which
I have some code that will change the background color of a specific label
I have the following code a.rollover { background-image: url('sprite.jpg'); display: block; width: 191px; height:

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.