【发布时间】:2013-04-09 23:45:04
【问题描述】:
我的主程序如下(伪代码):
public void main(String[] args) {
// produce lots of int[] data which is stored inside a list of hashmaps
List<HashMap<Integer, int[]>> dataArray1 = new
ArrayList<HashMap<Integer, int[]>>();
...
// create a new list of data, similar to dataArray1
// now we will write into dataArray2 and read from dataArray1
List<HashMap<Integer, int[]>> dataArray2 = new
ArrayList<HashMap<Integer, int[]>>();
while (true) {
if (exitCondition) break;
...
for index1, index2 in a set of indices {
int[] a1 = dataArray1.get(index1).get(key1);
int[] a2 = dataArray1.get(index2).get(key2);
int[] b = intersect a1 and a2;
int i = generateIndex(index1, index2);
int key = generateKey(key1, key2);
dataArray2.get(i).put(key, b);
}
}
// now we can remove dataArray1
dataArray1 = null;
// create a new list of data, similar to dataArray2
// now we will write into dataArray3 and read from dataArray2
List<HashMap<Integer, int[]>> dataArray3 = new
ArrayList<HashMap<Integer, int[]>>();
while (true) {
if (exitCondition) break;
...
for index1, index2 in a set of indices {
int[] a1 = dataArray2.get(index1).get(key1);
int[] a2 = dataArray2.get(index2).get(key2);
int[] b = intersect a1 and a2;
int i = generateIndex(index1, index2);
int key = generateKey(key1, key2);
dataArray3.get(i).put(key, b);
}
}
// now we can remove dataArray2
dataArray2 = null;
...
// and so on 20 times
}
我的问题是,在某些时候dataArrayk 对于某些k > 1 变得很重(比如 20 Gb),因此不可能将其存储在内存中。我可以将int[] 更改为BitSet 但这无济于事,内存消耗更多。
解决方案是使用数据库或文件系统。你会推荐使用什么?我需要性能(时间执行),内存无所谓。如果您的经验是数据库,那么请推荐用于处理特定(哪个?)数据库的最快接口,无论是 bd4 (Berkeley db)、postgresql 还是其他。如果是文件系统,请推荐最快的接口(文件库)。
关于读写统计:
在我的代码的每个 while 循环中,我执行的读取次数比写入次数多 3 倍,例如:对于一级 k,我从 dataArray_k 读取 12000 次并写入 dataArray_(k+1) 4000 次。
我可以将来自List<HashMap<Integer, int[]>> dataArray1 的每个哈希图存储在单独的文件中。
【问题讨论】:
-
告诉我们 yoi 想要做什么会更有意义。 20GB 的数据很多
-
简单的答案 - 我有一百万个代表生物细胞传感器数据的数组,我需要以某种方式相交,所以在每个
dataArrayk中,我存储了从以前的dataArray_k-1。我的程序已经过优化,所以不会占用这么多内存。 -
它取决于您的生物算法,如果计算仅参考附近的值,则磁盘方法将起作用。如果计算总是访问所有值,最好购买更多内存,同时进行优化,例如短数组。
-
你的生物数据可以被认为是二维的吗?还是 3 维?
-
我觉得基于磁盘的存储具有巨大的缓存是有道理的。那么数据的访问路径是什么?这对性能有多重要?特定数据块的请求是否比其他数据块更频繁?您需要事务管理吗?显示的伪代码是您在该应用程序中对数据执行的唯一操作吗?在启动数据库和设计模式等之前,我会考虑一种基于文件的方法,它具有智能数据结构和某种基于内存的缓冲/缓存/预读机制。
标签: java database performance memory filesystems