1. 程式人生 > >淺談並小結java記憶體洩漏

淺談並小結java記憶體洩漏

public class B extends A{
	
	public void test(){
		new Thread(new Demo()).start();
	}
	
	@Override
	public void dosth() {
		// TODO Auto-generated method stub
		System.out.println("doB");
	}
	
class Demo implements Runnable{
	
		
		public Demo(){
			try {
				Field field = getClass().getDeclaredField("this$0");
				field.setAccessible(true);
				field.set(this, null);
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			
			dosth();
			
		}
	}
}
執行異常如下