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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:51:28+00:00 2026-05-20T02:51:28+00:00

I have encountered a somewhat baffling problem with the simple task of filling an

  • 0

I have encountered a somewhat baffling problem with the simple task of filling an Array dynamically in Java. The following is a snapshot from where the problem originates:

entries = new Object[ (n = _entries.length + 1) ] ;

for(i = 0 ; i < n ; i++) {

            entry = ( i == (n - 1) )  ?  addition  :  _entries[i]  ;

            entries[i] = entry ;

            //...

}

Where _entries is a source Array (field of the class); entries is initialized as an Array of Objects

Object[] entries = null ;

and addition is the Object to be added (passed as an Argument to the method this code is in).


The code passes the compiler but results in a memory-leak when called. I was able to narrow down the cause to the line where the code attempts to fill the new Array

 entries[i] = entry ;

however, I cannot think of any reason why this would cause a memory-leak. I’m guessing the root of the issue must be either an extremely stupid fault on my part or an extremely arcane problem with Java. 🙂

If you need more background let me know.

Edit:

Tomcat’s log tells me:

A web application appears to have started a thread named … but has failed to stop it.
This is very likely to create a memory leak.

Other than that obviously the page loading the class does not finish loading or loads very slowly.

Edit:

The problem might be somewhere else (at a more expected location) after all. Apparently Tomcat wasn’t loading the class files all the time when I tried to pin down the faulty code and this mislead me a bit. I now suspect a infinte for-each loop caused by a defective Iterator implementation up in the call stack to be at fault.
In any case, thanks for your input! Always much appreciated!


I will use a Collection (probably a Vector) instead of an Array as a work-around; still, I’d like to know what the problem here is.

TIA,

FK82

  • 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-20T02:51:29+00:00Added an answer on May 20, 2026 at 2:51 am

    I cannot see a memory leak, but your code is more complicated than it needs to be. How about this:

    newLength = $entries.length + 1;
    entries = new Object[ newLength ] ;
    
    for(i = 0 ; i < newLength - 1 ; i++) {
            entries[i] = $entries[i];
            //...
    }
    
    entries[ newLength - 1 ] = addition;
    

    No need to check if you are at the last entry all the time and you could use a array copy method as suggested by Alison.

    Think of this post as a comment. I just posted it as an answer because I don’t know how code is formatted in comments…

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

Sidebar

Related Questions

I have encountered a problem with creating a thumbnail from an uploaded image file,
I have encountered a problem in one of my Java projects, which causes bugs.
I have encountered a problem in my program that has me somewhat stumped. I
I have encountered a slightly unusual problem. Consider the following code: class parser {
I have encountered a problem where socket.emit will not send an array. When the
I have encountered a weird problem, when i built my last ad-hoc version for
I have encountered a most annoying problem that occurs on the PWD variable when
I have encountered a rather nasty problem with the DataGridView control (Windows.Forms, .NET Framework
I have encountered a problem twice now whereby a producer thread produces N work
I have encountered a problem of placing data into the Table View. Here is

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.