2015-11-14 28 views
0

當我運行這個類時,會發生內存不足錯誤。該程序用於檢測有向/無向圖中的週期。該錯誤消息在100%cpu和內存使用率之後。如何避免圖表循環檢測程序中的「內存不足Java堆空間」?

package Project; 

// Imports for Java API 
import java.util.*; 
import java.util.stream.Collectors; 
import java.util.stream.Stream; 
import java.sql.*; 
import org.jgrapht.alg.CycleDetector; 
import org.jgrapht.alg.DijkstraShortestPath; 
import org.jgrapht.alg.DirectedNeighborIndex; 
import org.jgrapht.alg.FloydWarshallShortestPaths; 
import org.jgrapht.alg.cycle.JohnsonSimpleCycles; 
import org.jgrapht.alg.cycle.SzwarcfiterLauerSimpleCycles; 
import org.jgrapht.*; 
import org.jgrapht.graph.DefaultEdge; 
import org.sqlite.SQLite; 
import org.jgrapht.graph.DefaultDirectedGraph; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class Sort { 

    final static String fileVertices ="C:/Users/a2z/Desktop/Video/Processed data/Author_Vertices/JLee_Vertices.txt"; 
    final static String fileEdges ="C:/Users/a2z/Desktop/Video/Processed data/Author_Edges/JLee_Edges.txt"; 
    public static void main(String[] args) throws FileNotFoundException, IOException { 

     // // Create Directed Empty Graph Instance and class Object 
     ////////////////////////////////////////////////////////////////////////////////// 
     //DirectedGraph<String, DefaultEdge> directedGraph = new DefaultDirectedGraph<> (DefaultEdge.class); 
     DirectedGraph<String, DefaultEdge> diGraph = new DefaultDirectedGraph<> (DefaultEdge.class); 
     // Addsvertices and edges in the graph 
     fileLoad.addVertices(fileVertices, (DirectedGraph<String, DefaultEdge>) diGraph); 
     System.out.println("Vertices added in the graph!!!"); 
     fileLoad.addEdges(fileEdges, (DirectedGraph<String, DefaultEdge>) diGraph); 
     System.out.println("Edges added in the graph!!!"); 
     SzwarcfiterLauerSimpleCycles s = new SzwarcfiterLauerSimpleCycles(diGraph); 
     List<DefaultEdge> cycles = s.findSimpleCycles(); 
     System.out.println(cycles); 
    } 

    public void addVertices(String fp, DirectedGraph<String, DefaultEdge> digra) throws FileNotFoundException, IOException 
    { 
     try (BufferedReader br = new BufferedReader(new FileReader(fp))) 
     { 
      String line=""; 
      while ((line = br.readLine()) != null) { 
       digra.addVertex(line); 
      } 

     } 

    } 

    public void addEdges(String fpe, DirectedGraph<String, DefaultEdge> digrae) throws FileNotFoundException, IOException 
    { 
     try (BufferedReader br = new BufferedReader(new FileReader(fpe))) 
     { 
      String line=""; 
      int counter = 1; 
      while ((line = br.readLine()) != null) { 
       String [] s = line.split(","); 
       for(int i=0;i<s.length;i=i+2){ 
        digrae.addEdge(s[i], s[i+1]); 
        counter++; 
        //System.out.println(counter); 
       } 
      } 
     } 

    } 

} // End of Class 

JLEE頂點文件是這樣的1379個不同的頂點,即

H R Park 
T Butzer 
Jaiyong Lee 
Kwang Sik Eom 
A Townsend 
M Jang 
Dong-Min Kim 
Daya Atapatta 
R Machiraju 
Mann Ho Lee 
K P Fung 
R Cox 
G Yang 
Edmund H Durfee 
M Kim 
Ching-Ren Lee 
Jabeom Gu 
Arjun Kapur 
D W Kim 
Gurpreet Dhillon 
M Papazoglou 
Yongho Cho 
Lisa Masterman 
T W Kang 
Jin-Seok Chae 
Y -W Chang 
Hai Fang 
S W Jeong 
Y S Kwon 
Zehra Sura 

JLEE_Edges文件是這樣的有14440個邊緣,即

Tosiyasu L Kunii,J Lee 
Yan Solihin,Jaejin Lee 
Jaejin Lee,Yan Solihin 
Yan Solihin,Josep Torrellas 
Josep Torrellas,Yan Solihin 
Jaejin Lee,Josep Torrellas 
Josep Torrellas,Jaejin Lee 
Yan Solihin,Jaejin Lee 
Jaejin Lee,Yan Solihin 
Yan Solihin,Josep Torrellas 
Josep Torrellas,Yan Solihin 
Jaejin Lee,Josep Torrellas 
Josep Torrellas,Jaejin Lee 
Yan Solihin,Jaejin Lee 
Jaejin Lee,Yan Solihin 
Yan Solihin,Josep Torrellas 
Josep Torrellas,Yan Solihin 
Jaejin Lee,Josep Torrellas 
Josep Torrellas,Jaejin Lee 
J Lee,Josep Torrellas 
Josep Torrellas,J Lee 
J Lee,Yan Solihin 
Yan Solihin,J Lee 
Josep Torrellas,Yan Solihin 
Yan Solihin,Josep Torrellas 
J Lee,Josep Torrellas 
Josep Torrellas,J Lee 
J Lee,Yan Solihin 
+0

試圖運行你的代碼來找出最新錯誤,但有很多丟失的依賴關係... – coderrick

回答