1

I am thinking of creating a complex data type myself, but just not sure of the cost of it. Let's say , I have 3 lists, name,age,gender, List<String> name = new ArrayList<String>; List<Integer> age = new ArrayList<Integer>; List<String> gender = new ArrayList<String>; I would like to combine each element of these lists together, something like this:

public class Person {
    private String name;
    private int age;
    private String gender;
    public void Person(String name,int age,String gender){
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    public void getName () {
    return name;
    }
    public void getAge () {
    return age;
    }
    public void getGender () {
    return gender;
    }
}

then I can create the object that contains these information: Person person1 = new Person("John",22,"Male"); But the thing is the list of name is so big that may have 1,000,000 names(also the list of age and gender),meaning I would need to create 1,000,000 objects of Person. Is this a good idea to pass object containing name,age and gender to another class or I should just pass these name,age,gender separately?

How big would a object containing name,age and gender be, compared to the cost of String name, int age and String gender added together?

user207421
  • 305,947
  • 44
  • 307
  • 483
Xuzheng Wang
  • 531
  • 5
  • 17
  • Please see http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – bobs_007 Aug 14 '15 at 00:32
  • 3
    You shouldn't be loading 1 million objects into memory at once. If you are doing that, you probably need to redesign your application, because at best, you will see some serious performance issues. – forgivenson Aug 14 '15 at 00:32
  • I would heed @forgivenson's suggestion.. – Sid Aug 14 '15 at 00:38
  • 1
    as @forgivenson said, you should not load 1 million objects in the memory at once, in your case you might need to use a database to store and retrieve objects. – Flowryn Aug 14 '15 at 03:12

4 Answers4

3

Don't do premature optimizations. The most clear way will be to have one list of Person objects. If you actually experience performance problems with this part, then think about how to optimize it.

About the memory overhead of objects:

  • each object by itself has overhead of 8 bytes
  • String fields of the objects will occupy the same size as your current arrays
  • int field of the object will occupy less space comparing to the elements of List<Integer> 4 bytes vs 4 + 16 bytes

So, in summary, you will save 12 bytes on each record if you go with grouping Lists into single List of objects.

UPD the actual saving probably will be 8 bytes because of the alignment

Community
  • 1
  • 1
Aivean
  • 10,692
  • 25
  • 39
0

If you think about it, it would make more sense to do the list of Person instead of 3 separate lists. If you were to make 3 lists it would be complicated to keep them together and you would still be holding an object for each String name, int age, and String gender.

All an array list of people would do is hold references for the people so it would be about the same size it would just be in one list to make it less confusing since each person holds exactly the same amount of data as the three lists added together.

Josh
  • 187
  • 1
  • 1
  • 9
  • I see, I could make name, age, and gender together as a string, like "John,22,Male". then I have a list of String instead of a list of Person object containing "John",22,"Male" separately. – Xuzheng Wang Aug 14 '15 at 00:53
  • that would work as well and would be smaller than your previous mentioned lists, it is just my preference to separate variables so i don't have to extract data later on. – Josh Aug 14 '15 at 00:57
0

Your original code is already allocating an extra object (Integer), so changing to Person will not change your memory consumption much, since age is a primitive, not an object.

You can squeeze memory by ensuring that you don't allocate a lot of duplicate strings, depending on how the gender is obtained. If it is from a parsing operation, you're likely allocating a new "Male" string for every male person. Consider changing gender to an Enum.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

The bottom line is, for your application such cost is trival.

Each Java object has 8 bytes overhead storing its object class information, GC indicator, etc. By design Java chooses to add all auxiliary info to facilitate easy programming and debugging rather than memory efficiency. So you are creating an object with such overhead and 3 more pointers which gives you less than 8 + 3 * 8 bytes on 64-bit box which is 32 bytes per object (approximately). Even in large scale high performance systems that's never a problem given you have already chosen Java as your system language.

Alex Suo
  • 2,977
  • 1
  • 14
  • 22