2016-10-11 5 views
0

이 작은 스크립트를 ping google.pt에 작성하고 ping 결과로 무언가를하십시오. 문제는 내가 스크립트를 잠시 실행 시키면 더 많은 RAM을 사용한다는 것입니다.메모리 누수 문제를 해결하려면 어떻게해야합니까?

실수를 찾을 수없는 것 같습니다. 도와 주실 수 있습니까?

public static void main(String[] args) { 
    String ip = "google.pt -t -4"; 
    int pingRetrieved = 0; 

    //window that displays the ping 
    s = new Square(); 


    String pingCmd = "ping " + ip; 
    try { 


     Runtime r = Runtime.getRuntime(); 

     Process p = r.exec(pingCmd); 

     BufferedReader in = new BufferedReader(new 
     InputStreamReader(p.getInputStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) { 
      //System.out.println(inputLine); 
      pingRetrieved = getPingValueFromPingResult(inputLine); 
      takeActionFromPingValue(pingRetrieved); 
     } 
     in.close(); 

    } catch (IOException e) { 
     System.out.println(e); 
    } 


} 

편집 : getPingValue 방법 :

public void printNumber(int ping){ 

    this.getContentPane().removeAll(); 
    JLabel jl = new JLabel(); 
    Font f; 
    if(ping == 0){ 
     this.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth()-200), 0); 
     jl = new JLabel("Connection Error"); 
     f = new Font("Fixedsys", Font.PLAIN, 25); 
    }else{ 
     this.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth()-100), 0); 
     jl = new JLabel(ping+"ms"); 
     f = new Font("Fixedsys", Font.PLAIN, 25); 
    } 
    jl.setFont(f); 
    jl.setForeground(Color.MAGENTA); 
    this.getContentPane().add(jl, java.awt.BorderLayout.NORTH); 
    this.pack(); 

} 
+0

'takeActionFromPingValue'에는 무엇이 포함되어 있습니까? – Enzokie

+0

다른 메소드는'getPingValueFromPingResult'와'takeActionFromPingValue'을 어떻게합니까? –

+0

여기에 게시 된 코드 부분에는 명백한 문제가 없습니다. 누락 된 방법 일 가능성이 큽니다. – f1sh

답변

0

ping 명령은 일반적으로 영원 따라서 귀하의 실행 :

private static int getPingValueFromPingResult(String inputLine) { 
    String[] splitString; 
    String timeParameter; 
    if (inputLine.contains("Reply")) { 
     splitString = inputLine.split(" "); 
     timeParameter = splitString[4]; 
     timeParameter = (timeParameter.split("="))[1]; 
     timeParameter = timeParameter.replace("ms", ""); 
     return Integer.parseInt(timeParameter); 
    } 
    return 0; 
} 

와 가지고 actionFromPingValueMethod 그냥 내가 만든 JFrame의에이를 호출 조건

while ((inputLine = in.readLine()) != null) 

은 시간 초과 (-t으로 지정됨)가 트리거 될 때만 false입니다. -c count 매개 변수를 사용하여 ping 요청 수를 제한해야합니다.

관련 문제