views:

242

answers:

8

I've currently got a spreadsheet type program that keeps its data in an ArrayList of HashMaps. You'll no doubt be shocked when I tell you that this hasn't proven ideal. The overhead seems to use 5x more memory than the data itself.

This question asks about efficient collections libraries, and the answer was use Google Collections. My follow up is "which part?". I've been reading through the documentation but don't feel like it gives a very good sense of which classes are a good fit for this. (I'm also open to other libraries or suggestions).

So I'm looking for something that will let me store dense spreadsheet-type data with minimal memory overhead.

  • My columns are currently referenced by Field objects, rows by their indexes, and values are Objects, almost always Strings
  • Some columns will have a lot of repeated values
  • primary operations are to update or remove records based on values of certain fields, and also adding/removing/combining columns

I'm aware of options like H2 and Derby but in this case I'm not looking to use an embedded database.

EDIT: If you're suggesting libraries, I'd also appreciate it if you could point me to a particular class or two in them that would apply here. Whereas Sun's documentation usually includes information about which operations are O(1), which are O(N), etc, I'm not seeing much of that in third-party libraries, nor really any description of which classes are best suited for what.

+4  A: 

Some columns will have a lot of repeated values

immediately suggests to me the possible use of the FlyWeight pattern, regardless of the solution you choose for your collections.

Brian Agnew
While not addressing the main problem, this did nudge me to finally figure out how to flyweight Strings in java properly. Thanks. http://stackoverflow.com/questions/3972841/when-is-it-beneficial-to-flyweight-strings-in-java
bemace
+2  A: 

Trove collections should have a particular care about space occupied (I think they also have tailored data structures if you stick to primitive types).. take a look here.

Otherwise you can try with Apache collections.. just do your benchmarks!

In anycase, if you've got many references around to same elements try to design some suited pattern (like flyweight)

Jack
Trove won't work for me because I'm not using primitives. I see HashedMap in Apache collections is a "general purpose alternative" but they don't give any explanation of what's different from regular HashMap. Have any insight there?
bemace
Actually, I see it does mention adding iteration functionality. However, my issue is with performance not missing features.
bemace
A: 

keeps its data in an ArrayList of HashMaps
Well, this part seems terribly inefficient to me. Empty HashMap will already allocate 16 * size of a pointer bytes (16 stands for default initial capacity), plus some variables for hash object (14 + psize). If you have a lot of sparsely filled rows, this could be a big problem.

One option would be to use a single large hash with composite key (combining row and column). Although, that doesn't make operations on whole rows very effective.

Also, since you don't mention the operation of adding cell, you can create hashes with only necessary inner storage (initialCapacity parameter).

I don't know much about google collections, so can't help there. Also, if you find any useful optimization, please do post here! It would be interesting to know.

Nikita Rybak
I assure you it *is* terribly inefficient, that's why I'm here :) In my case sparse rows aren't a big issue.
bemace
A: 

So I'm assuming that you have a map of Map<ColumnName,Column>, where the column is actually something like ArrayList<Object>.

A few possibilities -

  • Are you completely sure that memory is an issue? If you're just generally worried about size it'd be worth confirming that this will really be an issue in a running program. It takes an awful lot of rows and maps to fill up a JVM.

  • You could test your data set with different types of maps in the collections. Depending on your data, you can also initialize maps with preset size/load factor combinations that may help. I've messed around with this in the past, you might get a 30% reduction in memory if you're lucky.

  • What about storing your data in a single matrix-like data structure (an existing library implementation or something like a wrapper around a List of Lists), with a single map that maps column keys to matrix columns?

Steve B.
Actually each record is a Map<Field,Object> which Object is the values of each field. All the records are contained in an ArrayList. Memory is definitely an issue. Loading a file of 50MB can sometimes exceed 1GB of memory, which leads me to believe that my current implementation is horribly naive.
bemace
I will do some testing with different options; what I'm trying to do here is narrow the field to a few specific classes within different libraries that I can compare.
bemace
A: 

From your description, it seems that instead of an ArrayList of HashMaps you rather want a (Linked)HashMap of ArrayList (each ArrayList would be a column).

I'd add a double map from field-name to column-number, and some clever getters/setters that never throw IndexOutOfBoundsException.

You can also use a ArrayList<ArrayList<Object>> (basically a jagged dinamically growing matrix) and keep the mapping to field (column) names outside.

Some columns will have a lot of repeated values

I doubt this matters, specially if they are Strings, (they are internalized) and your collection would store references to them.

leonbloy
+2  A: 

Guava does include a Table interface and a hash-based implementation. Seems like a natural fit to your problem. Note that this is still marked as beta.

Willi
The Guava Table implementations are implemented as a Map with Map values. As a result, they won't reduce memory usage.
Jared Levy
@Jared I would assume that would depend on the implementation of Map used?
bemace
@Jared, you are right.
Willi
@bemace The primary Map implementations -- HashMap and TreeMap -- have significant memory overhead when there are many of them. I created an array-based table, which is memory-efficient when dense but requires fixed row and column keys, but it hasn't yet been open-sourced in Guava.
Jared Levy
+2  A: 

Assuming all your rows have most of the same columns, you can just use an array for each row, and a Map<ColumnKey, Integer> to lookup which columns refers to which cell. This way you have only 4-8 bytes of overhead per cell.

If Strings are often repeated, you could use a String pool to reduce duplication of strings. Object pools for other immutable types may be useful in reducing memory consumed.

EDIT: You can structure your data as either row based or column based. If its rows based (one array of cells per row) adding/removing the row is just a matter of removing this row. If its columns based, you can have an array per column. This can make handling primitive types much more efficent. i.e. you can have one column which is int[] and another which is double[], its much more common for an entire column to have the same data type, rather than having the same data type for a whole row.

However, either way you struture the data it will be optmised for either row or column modification and performing an add/remove of the other type will result in a rebuild of the entire dataset.

(Something I do is have row based data and add columnns to the end, assuming if a row isn't long enough, the column has a default value, this avoids a rebuild when adding a column. Rather than removing a column, I have a means of ignoring it)

Peter Lawrey
If the original poster's values really are dense, this will work great. Object[][] or List<Object[]>. Don't discount the old standbys! Add Field#getNumber() and you're golden. As for duplication of values, guava-libraries 'Interner' interface would seem to fit the bill.
Darren Gilroy
Yes, that is what I had in mind.
Peter Lawrey
Not a bad idea but how do you handle adding and remove rows/columns with that sort of structure?
bemace
A: 

I've been experimenting with using the SparseObjectMatrix2D from the Colt project. My data is pretty dense but their Matrix classes don't really offer any way to enlarge them, so I went with a sparse matrix set to the maximum size.

It seems to use roughly 10% less memory and loads about 15% faster for the same data, as well as offering some clever manipulation methods. Still interested in other options though.

bemace