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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:09:01+00:00 2026-05-17T19:09:01+00:00

static private ArrayList seriesColors = new ArrayList(); public Audiogram(int widthParm, int heightParm) throws Exception

  • 0
static private       ArrayList   seriesColors      = new ArrayList();

public Audiogram(int widthParm, int heightParm)
            throws Exception
    {
        super(widthParm, heightParm);
        seriesColors.add(new Color(  0,   0, 255));

        // Set the default settings to an industrial audiogram
        setType(INDUSTRIAL_AUDIOGRAM);
    }

I have some five methods like this, but would like to know whether this code above could cause memory leakage since the seriesColors is static.

If there is a memory leakage, then what is the solution for that?

private static final ColorModel  rgbModel  = ColorModel.getRGBdefault();

public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize )
    {
        if ( model == rgbModel ) {
            try {
                encodePixelsWrapper( x, y, w, h, pixels, off, scansize );
                }
            catch ( IOException e ) {
                iox = e;
                stop();
                return;
                }
            }
            else {
                int[] rgbPixels = new int[w];
                for ( int row = 0; row < h; ++row ) {
                    int rowOff = off + row * scansize;
                    for ( int col = 0; col < w; ++col ) {
                        rgbPixels[col] = model.getRGB( pixels[rowOff + col] );
                        }
                    try {
                        encodePixelsWrapper( x, y + row, w, 1, rgbPixels, 0, w );
                        }
                    catch ( IOException e ) {
                        iox = e;
                        stop();
                        return;
                        }
                    }
                }
    }

 public static ColorModel getRGBdefault() {
    if (RGBdefault == null) {
        RGBdefault = new DirectColorModel(32,
                          0x00ff0000,   // Red
                          0x0000ff00,   // Green
                          0x000000ff,   // Blue
                          0xff000000    // Alpha
                          );
    }
    return RGBdefault;
    }

Out of these two code which one has serious flaws?

  • 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-17T19:09:01+00:00Added an answer on May 17, 2026 at 7:09 pm

    Static variables are shared between all the instances of a class. (An instance is created using the “new” operator.)

    In these examples; it’s probably not a good idea to use a static (instance variable) to store colours in, as the instances will interfere with each other. That variable should be changed to an “ordinary” instance variable.

    The final static colorModel in the second example is perfectly fine; It’s an immutable object (at least the interface is immutable) and the methods are most likly threadsafe and can be used by a lot of instances at the same time.

    Note that the first problem is not a “memory leak”. You might say that hte instances unintentionally is leaking data between them, but’s its not a classic memory leak where active objects are unintenonally keeping references to “dead” objects.

    IF seriesColors for some reason should contain a color for each instance created. (Which is probably a stupid design) the access to the arraylist mustbe syncronized in some way. But I think that is out of scope here…

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

Sidebar

Related Questions

No related questions found

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.