简体   繁体   中英

Is memory allocated once or at every instantiation of Objects in java

I made a little program to hold my X and Y values. here is my code:

public class ChartData 
    {
        private Object x, y; 

        public ChartData(Object x, Object y)
        {
            this.x = x;
            this.y = y;
        }

        public Object getX() 
        {
            return x;
        }

        public Object getY() 
        {
            return y;
        }

        public boolean equals(Object obj)
        {
            if (obj != null && obj instanceof ChartData) 
            {
                final ChartData other = (ChartData) obj;
                if (this.getX().equals(other.getX()) && this.y == null && other.y == null) 
                {
                    return true;
                }
                else if (this.getX().equals(other.getX()) && this.y.equals(other.y)) 
                {
                    return true;
                }
            }
            return false;
        }

        public int hashCode()
        {
            int hashCode = getX().hashCode();
            hashCode = (y != null) ? hashCode + y.hashCode() : hashCode ;
            return hashCode;
        }
    }

I would like to sum the values of the ChartData Object(ie it holds X and Y values). For Example:-

LinkedHashMap<ChartData, Object> datasource = new LinkedHashMap<ChartData, Object>();
        while(data.next())
        {            
            Object x = data.getValue(1);
            Object y = data.getValue(2);
            Double value = data.getValue(3);
            ChartData cateVal = new ChartData(x, y);
            Double currentValue = (Double) datasource.get(cateVal);
            if(currentValue != null)
            {
                datasource.put(cateVal, currentValue + value);
            }
            else
            {
                datasource.put(cateVal, value);
            }
        }

there is a chance to create a new object for the same X and Y values.Is it actually creates a new object for every instantiation or else it uses previously created memory.Please explain me.

You HashMap key, ie, ChartData cateVal = new ChartData(x, y); creates a new object everytime it is called.

There is no automatic memory management, and I don't think there is any way to overcome this using HashMaps, but you should not worry about it, creation of such objects is very cheap.

I don't know if it is possible in general to get a mapping from (X, Y) -> Z, where X and Y are arbitrary objects, without creating an object that holds (X, Z). Maps in Java are from a single object to object. If X and Y are integers, or can be mapped to integers, you can create an array Z[][] indexed by X and Y. In you case it would mean that x and y inside CharData should be integers, which I assume what you want:

Object z = datasource[x][y];
if (z == null) {
    z = new ChartData(x,y);
    datasource[x][y] = z;
}

If the x and y values are sparse and possibly large, a better approach than the 2D array is needed.

Consider a Map<Integer, Map<Integer, ChartData>> as your cache. Very Skeleton code:

Map ymap = xmap.get(theXCoordinate);
if (ymap != null)
   chartData = ymap.get(theYCoordinate);

If chartData is there, return it, else add to the cache creating and putting the inner map if needed.

Note : unless you have a lot of duplicate ChartDatas, this probably uses more memory than just creating the duplicates. Exercise left for the reader as to where is the cutoff for now. :-)

v2 : Another idea I thought of later:

Create a Long holding your x and y coordinates, and use that as the key into a Map<Long, ChartData> . If your xs and ys are usually small, you could test that they are both less than 2^16 and use an Integer instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM