当前位置: 首页 > 知识库问答 >
问题:

线程“dispatcher-event-loop-1”Java.lang.OutOfMemoryError中出现异常:Java堆空间

甘学潞
2023-03-14

我使用Google云平台进行Spark(2.0.2)图像处理。当我执行我的代码(Java)时,我得到这个错误:

[Stage1:>(0+0)/2]17/10/15 13:39:44警告org.apache.spark.scheduler.TaskSetManager:Stage1包含一个非常大的任务(165836 KB)。建议的最大任务大小为100 KB。

在哪里以及如何增加Java堆空间?

我的程序:

public static void main(String[] args) {
  try{

          //Configuration de Spark .... 
          SparkSession spark = SparkSession
                .builder()
                .appName("Features")
                .getOrCreate();

          JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());

          //Configuration HBase  .... 
          String tableName = "Descripteurs";
          Configuration conf = HBaseConfiguration.create();
          conf.addResource(new Path("/home/ibtissam/hbase-1.2.5/conf/hbase-site.xml"));
          conf.addResource(new Path("/home/ibtissam/hbase-1.2.5/conf/core-site.xml"));
          conf.set(TableInputFormat.INPUT_TABLE, tableName);

          Connection connection = ConnectionFactory.createConnection(conf);
          Admin admin = connection.getAdmin(); 
          Table tab = connection.getTable(TableName.valueOf(tableName));

          for (int n=0; n<10; n++) {
              List<String> images =new ArrayList<>();
              String repertory_ = "/home/ibtissam/images-test-10000/images-"+n+"/"; 
              File repertory = new File(repertory_);
              String files[] = repertory.list(); 

              for(int k=0; k<10;k++){
                  ExecutorService executorService = Executors.newCachedThreadPool();
                  List<MyRunnable> runnableList = new ArrayList<>();

                  for(int i=k*100; i<(k+1)*100 ; i++){
                        MyRunnable runnable = new MyRunnable(repertory_+files[i]); 
                        runnableList.add(runnable);
                        executorService.execute(runnable);
                  }
                  executorService.shutdown();

                  while(!executorService.isTerminated()){}

                  for (int i=0; i<runnableList.size(); i++) {
                      images.add(runnableList.get(i).descripteurs_);
                  }
              }

          JavaRDD<String> rdd = jsc.parallelize(images, 2000);

          //Calcul des descripteurs
          JavaPairRDD<String,String> rdd_final = rdd.mapToPair(new PairFunction<String,String,String>() {
                @Override
                public Tuple2<String,String> call(String value) {

                  String strTab[] = value.split(","); 
                  int h = Integer.parseInt(strTab[1]);
                  int w = Integer.parseInt(strTab[2]);
                  String type = strTab[3]; 
                  String nom = strTab[0];
                  String key = nom+"-"+h+"-"+w+"-"+type;

                  // Conversion de String >> Mat
                  Mat image = new Mat(h, w, 16);
                  UByteRawIndexer idx = image.createIndexer();
                  int indice = 4;
                  for (int i =0;i<h;i++) {
                    for (int j=0;j<w;j++) {
                      idx.put(i, j, Integer.parseInt(strTab[indice]));
                      indice = indice++;
                    }
                  }

                  // Calcul des features 
                  SIFT sift = new SIFT().create(); 
                  KeyPointVector keypoints = new KeyPointVector();
                  Mat descriptors = new Mat();

                  image.convertTo(image, CV_8UC3);

                  sift.detect(image, keypoints);

                  KeyPointVector keypoints_sorted = new KeyPointVector(); 
                  keypoints_sorted = sort(keypoints);
                  KeyPointVector  keypoints_2 = new KeyPointVector((keypoints_sorted.size())/4); 
                  for (int k = 0; k < (keypoints_sorted.size())/4; k++){
                      keypoints_2.put(k, keypoints_sorted.get(k));  
                  }

                  sift.compute(image,keypoints_2,descriptors);
                  image.release(); 

                  int hDes = descriptors.size().height();
                  int wDes = descriptors.size().width();
                  key = key +"-"+hDes+"-"+wDes+"-"+descriptors.type();

                  while(hDes ==0 | wDes==0){
                      SIFT sift_ = new SIFT().create(); 
                      KeyPointVector keypoints_ = new KeyPointVector();

                      sift.detect(image, keypoints_);

                      KeyPointVector keypoints_sorted_ = new KeyPointVector(); 
                      keypoints_sorted_ = sort(keypoints_);
                      KeyPointVector  keypoints_2_ = new KeyPointVector((keypoints_sorted_.size())/4); 
                      for (int k = 0; k < (keypoints_sorted_.size())/4; k++){
                          keypoints_2_.put(k, keypoints_sorted_.get(k));  
                      }

                      sift_.compute(image,keypoints_2_,descriptors);
                  }

                  // Converion des features => String 
                  String featuresStr = new String("");
                  FloatRawIndexer idx_ = descriptors.createIndexer(); 
                  int position =0;

                  for (int i =0;i < descriptors.size().height();i++) {
                    for (int j =0;j < descriptors.size().width();j++) {

                      if (position == 0) {
                          featuresStr = String.valueOf(idx_.get(position))+",";
                      }
                      if (position == ((descriptors.size().height()*descriptors.size().width())-1) ){
                          featuresStr = featuresStr + String.valueOf(idx_.get(position));                  
                      }else{
                          featuresStr = featuresStr + String.valueOf(idx_.get(position))+","; 
                      }
                      position++;
                    }
                  }
                  descriptors.release(); 
                  Tuple2<String, String> tuple = new Tuple2<>(key, featuresStr);
                  return tuple;
                } 
              });

              System.out.println("Fin de calcul des descripteurs  .... ");

              List<Tuple2<String,String>> liste = rdd_final.collect();

              System.out.println("Insertion dans hbase .... \n");
              for (int b=0; b<liste.size(); b++) {

                    String metadata[] = liste.get(b)._1().split("-"); 
                    String data = liste.get(b)._2();
                    // Row 
                    byte [] row = Bytes.toBytes(liste.get(b)._1());

                    // Family
                    byte [] family1 = Bytes.toBytes("Metadata");
                    byte [] family2 = Bytes.toBytes("Data");

                    // Qualifiers
                    byte [] height = Bytes.toBytes("height");
                    byte [] width = Bytes.toBytes("width");
                    byte [] colorSpace = Bytes.toBytes("colorSpace");
                    byte [] name = Bytes.toBytes("name");

                    byte [] features = Bytes.toBytes("features");

                    // Create Put
                    Put put = new Put(row);
                    put.addColumn(family1, height, Bytes.toBytes(metadata[5]));
                    put.addColumn(family1, width, Bytes.toBytes(metadata[6]));
                    put.addColumn(family1, name, Bytes.toBytes(metadata[0]+"-"+metadata[1]+"-"+metadata[2]+"-"+metadata[3]));
                    put.addColumn(family1, colorSpace, Bytes.toBytes(metadata[4]));
                    put.addColumn(family2, features, Bytes.toBytes(liste.get(b)._2()));
                    tab.put(put);
              }
            }
            jsc.close();

      }catch(Exception e){

        System.out.println(e);
      }
    }

共有1个答案

柯昱
2023-03-14

尝试将驱动程序堆增加“--driver-memory xxxxm”

 类似资料: