5

I’m using ArrayList<Integer> in my research project. I need to keep unknown number of integers in this list. Sometimes I need to update the list: remove existing records or add new records. As Integer is an object, it’s taking much more memory than only int. Is there any alternate way to maintain the list that will consume less memory than Integer?

Simulant
  • 19,190
  • 8
  • 63
  • 98
user1539090
  • 75
  • 1
  • 6

4 Answers4

5

Try an integer list implementation that is optimized for memory usage, such as the one from the Colt library:

http://acs.lbl.gov/software/colt/api/cern/colt/list/IntArrayList.html

Java Integer objects usually require more overhead than an int primitive, so you need an implementation that is space-optimized.

From Colt:

Scientific and technical computing, as, for example, carried out at CERN, is characterized by demanding problem sizes and a need for high performance at reasonably small memory footprint. [...]

Community
  • 1
  • 1
jspcal
  • 50,847
  • 7
  • 72
  • 76
0

You could use an array with int-s and write your own methods with the same logic, that ArrayList does. But IMO that is a bad idea - modern machines have enough memory to use Integer objects, trust me... :)

user
  • 3,058
  • 23
  • 45
  • Yes, array is the only way to keep primitive types – Anthony Oct 06 '12 at 20:38
  • Sorry but that's a stupid thing to say. My computer's memory is filled in a second. A 500% Overhead (or what it actually is) is just not acceptable when you store millions of integers. – Jo So Feb 25 '16 at 16:44
0

That depends on the language you use, but I assume it's Java. In Java, as you probably know, you can't use ints in ArrayList, because they are a primitive datatype. To use ints, you'd have to use regular arrays, which are fixed size. That means you need to create a new array of a larger size each time you add something, assuming that makes the number of elements larger than the array. You trade memory for complexity, as you have to write a lot more code and mess around with moving ints back and forth.

The reduced memory use is unlikely to be worth the work and the extra risk of bugs in implementing such a solution.

Johanna Larsson
  • 10,531
  • 6
  • 39
  • 50
  • 1
    (1) Arrays aren't "immutable", they're "fixed-size". ("Immutable" would imply that you can't even change their elements.) (2) You don't need to create an array of larger size *each* time you add something; you just need to keep track of the size of your list, which can be less than the size of the array. (This, in fact, is how the JDK `ArrayList` class works, except that it uses an `Object[]` instead of an `int[]`.) – ruakh Oct 06 '12 at 22:45
0

You should also think about an alternativ storage system to you ArrayList. As for the linking mechanisim every Element has a overhead consuming (sometimes) more memory as the value itself. Maybe you don't need them ordered. Have you thought about a Map oder a simple Set if this is applicable or implement your own data structure?

Simulant
  • 19,190
  • 8
  • 63
  • 98