2016-10-26 2 views
0

나는 내 프로젝트에 많은 정적 도우미 메서드를 가지고 있으며 종종 그들에게 문맥을 인수로 전달한다. 이와 같이 컨텍스트를 전달하는 것은 정적 메소드는 참조의 붙잡고 메모리 누출을 야기시킬 수있는 경우에 다음 두 가지 예정적 메서드에 컨텍스트를 전달하면 Android에서 메모리 누수가 발생할 수 있습니까?

private static bool SaveSetupDetails(Context context, string sftpAddress, string sftpUserName, string sftpPassword) 
{ 
    try 
    { 
     using (ISharedPreferences settings = PreferenceManager.GetDefaultSharedPreferences(context)) 
     using (ISharedPreferencesEditor editor = settings.Edit()) 
     { 
      editor.PutString("VePSFTPAddr", sftpAddress); 
      editor.PutString("VePSFTPUser", sftpUserName); 
      editor.PutString("VePSFTPPass", sftpPassword); 

      editor.Commit(); 
      return true; 
     } 
    } 
    catch (Exception e) 
    { 
     Log.Debug("SomeTag", "SomeActivity - SaveSetupDetails threw an exception: " + e.Message); 
     return false; 
    } 
} 

I가 궁금

public static bool IsCallActive(Context context) 
{ 
    AudioManager manager = (AudioManager)context.GetSystemService(Context.AudioService); 
    if (manager.Mode == Mode.InCall) 
    { 
     return true; 
    } 
    return false; 
} 

번째 예이다. 또는 메소드 실행이 완료된 후에 참조 해제됩니까?

답변

2

안녕 @Ali 자히드 당신이 정적 키워드를 사용하여 객체를 저장하지 않았기 때문에 당신이 그때는 드 참조 얻을 것이다 위의 두 가지 방법을 같이 사용하여 매개 변수의 컨텍스트를 전달하고하는 경우 클래스. 이 객체들만이 초기화하는 동안 클래스 이름 앞에 정적 키워드를 적용한 메모리에 저장됩니다. 예 :

static int a = 0; 
0

onDestroy()의 참조를 안전하게 등록 취소하고 메모리 누수를 피할 수 있습니다.

@Override 
protected void onDestroy() { 
super.onDestroy(); 
//unregister your references. 
} 

:

관련 문제