Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

java - does these code has memory leakage?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

why would you make two post's for the same question? As for your question in general static member variables can cause memory leaks if not handled properly. With properly i mean that those variables live as long as the app lives and you have to take care that for instance an arraylist deletes items which are no longer needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...