简体   繁体   English

为什么添加和删除片段会给我造成内存泄漏?

[英]Why is adding and removing a fragment giving me memory leak?

I am fairly new to android dev so any help is really appreciated! 我对android开发人员还很陌生,因此非常感谢您的帮助! I am getting this error 我收到此错误

12-30 08:14:32.391 1726-1741/? 12-30 08:14:32.391 1726-1741 /? E/StrictMode﹕ A resource was acquired at attached stack trace but never released. E / StrictMode:在附加的堆栈跟踪中获取了资源,但从未释放过。 See java.io.Closeable for information on avoiding resource leaks. 有关避免资源泄漏的信息,请参见java.io.Closeable。 java.lang.Throwable: Explicit termination method 'end' not called at dalvik.system.CloseGuard.open(CloseGuard.java:180) at java.util.zip.Inflater.(Inflater.java:82) at com.android.okhttp.okio.GzipSource.(GzipSource.java:62) at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:645) at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:827) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java) at com.google.android.gms.http.GoogleHttpClient.a(SourceFile:811) at com.google.android.gms.http java.lang.Throwable:未在com.android.java.util.zip.Inflater。(Inflater.java:82)的dalvik.system.CloseGuard.open(CloseGuard.java:180)处调用显式终止方法'end'。在com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:645)上的okhttp.okio.GzipSource。(GzipSource.java:62)在com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine上) com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)处的com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)处的.java:827)。 com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)上的com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)。 com.google.android.gms.http.GoogleHttpClient.a(SourceFile:811)处com.google.android.gms.http上的HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java) .GoogleHttpClient.a(SourceFile:776) at com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:676) at com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:660) at com.google.android.gms.auth.be.ja(SourceFile:220) at com.google.android.gms.auth.be.appcert.aa(SourceFile:263) at com.google.android.gms.auth.be.appcert.aa(SourceFile:132) at com.google.android.gms.auth.be.appcert.ba(SourceFile:43) at com.google.android.gms.auth.bba(SourceFile:62) at com.google.android.gms.auth.baa(SourceFile:120) at com.google.android.gms.auth.baa(SourceFile:61) at com.google.android.gms.auth.be.cron.AuthCronService.a(SourceFile:44) at com.google.android.gms.gcm.al.run(SourceFile:135) com.google.android.gms.http.GoogleHttpClient.a(SourceFile:776)com.com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:660)的。 com.google.android.gms.auth.be上的google.android.gms.auth.be.ja(SourceFile:220)。com.google.android.gms.auth.be上的com.google.android.gms.auth.be.appcert.aa(SourceFile:263)。 com.google.android.gms.auth.be.appcert.ba(SourceFile:43)处的com.google.android.gms.auth.bba(SourceFile:62)处的appcert.aa(SourceFile:132) com.google.android.gms.auth.baa(SourceFile:61)上的.android.gms.auth.baa(SourceFile:120)com.google.android.gms.auth.be.cron.AuthCronService.a(SourceFile :44)在com.google.android.gms.gcm.al.run(SourceFile:135)

and part of the code that's causing the error 以及导致错误的部分代码

 gameLayout.setOnTouchListener(
            new FrameLayout.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //add bullet upon click
                    bullet_fragment b_fragment = new bullet_fragment();
                    fragmentTransaction.add(R.id.game, b_fragment).commit();

                    //bullet moves around screen (some code here)

                    // removes bullet
                    fragmentTransaction.remove(b_fragment).commit();
                    return true;
                }
          g  }
    );

so every time the screen is touched a fragment is generated so it's possible for there to be multiples of the same fragment to exist on the screen, which is no problem I've tried searching a bit and found that remove() doesn't necessarily move the fragment to the GC, but the person didn't say how to completely destroy it. 因此,每次触摸屏幕时都会生成一个片段,因此屏幕上可能存在多个相同片段,这是我尝试搜索一下并发现remove()不一定有问题的问题将该碎片移至GC,但此人没有透露如何完全销毁它。 How can I do that then? 那我该怎么办呢? or is there another reason for that error to occur? 还是有其他原因导致该错误发生?

code of the fragment 片段代码

public class bullet_fragment extends Fragment {
View bulletView;

ImageView bulletObject;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    bulletView = inflater.inflate(R.layout.bullet_layout, container, true);
    bulletObject = (ImageView) bulletView.findViewById(R.id.bullet);

    return bulletView;

}
public void shoot(int x, int y){
    FrameLayout.LayoutParams b_params = new FrameLayout.LayoutParams(bulletObject.getLayoutParams());


    final int B_WIDTH= (int) (bulletObject.getWidth() * 0.5);
    final int B_HEIGHT= (int) (bulletObject.getHeight() * 0.5);
    b_params.setMargins(x - B_WIDTH,y - B_HEIGHT,0,0);
    bulletObject.setLayoutParams(b_params);
}

} }

The issue in the stacktrace is clearly caused by okhttp but it may not necessarily be the fault of okhttp. 堆栈跟踪中的问题显然是由okhttp引起的,但不一定是okhttp的错误。 The fact that this happens inside gms which is part of Google Services suggests that you likely have nothing to do with the issue. 这是Google Services的一部分gms内部发生的事实,这表明您可能与该问题无关。 Are you sure this log is for your process and not some system process? 您确定此日志用于您的进程而不是某些系统进程吗?

okhttp reference okhttp参考

picasso reference (as an example of how a library may be at fault) 毕加索参考资料(作为图书馆可能出现故障的示例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM