我正在使用Arraylist(Classroom是我的代码中的一个类),我在其中存储大量数据(很多课堂)。我在循环中使用这个Arraylist来提取单个教室并在其上运行算法。我的代码在循环开始时随机播放Arraylist集合,选择Arraylist中第0个位置的教室。我正在使用Collection.shuffle()函数进行shuffle。我的问题是我的代码在堆空间不足并且在运行期间停止。
代码片段如下所示
ArrayList<ClassroomName> names = new ArrayList<ClassroomName>(Arrays.asList(ClassroomName.values()));
for(int x=0;x<names.size();x++){
Collections.shuffle(names);
ClassroomName name = names.remove(0);
int classID = getClassID(name, location);
if(classID != -1){
c = new Classroom(classID);
if(c!=null){
//System.out.println("Call findEmptySlotsForClassroom for just checking. Not retreiving");
/*
* Check the capacity of the classroom and the required capacity
*/
DBAnnotation.annoate("classCap", "classroom", "ClassroomCapacity", true);
int classCap = c.getClassroomCapacity();
if(classCap >= expectedCapacity){
/*
* Find empty timeslot for the classroom
*/
times = c.findOpenSlotsForClassroom(timeSlotType);
/*
* If atleast one empty slot is found, return the classroom in which it was found
*/
if(times.size()>0){
DBAnnotation.annoate("loc", "classroom", "ClassroomLocation", true);
ClassroomLocation loc = c.getClassroomLocation();
DBAnnotation.annoate("cName", "classroom", "ClassroomName", true);
ClassroomName cName = c.getClassroomName();
System.out.println("Found a classroom with empty time slots:"+cName.toString()+" "
+ ""+ loc.toString());
break;
}
}
c = null;
}
}
}
我认为shuffle会导致大量内存使用。我不确定是什么导致内存溢出。如果有人可以提供帮助,请告诉我。如果需要,我愿意提供任何其他缺失的信息。
感谢。