Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我很有信心我的可降级类中有内存泄漏?_Java_Android_Xml - Fatal编程技术网

Java 我很有信心我的可降级类中有内存泄漏?

Java 我很有信心我的可降级类中有内存泄漏?,java,android,xml,Java,Android,Xml,当特定的活动加载时,我的堆从16mb左右跳到64mb左右!起初,我认为这是由于我在drawable dir中调整了大png的大小造成的,但我已经将它们调整为更小的大小,因为它们只显示得很小 我不明白的是,当我退出活动时,为什么堆大小没有减少,希望有人能给我指出解决这个问题的正确方向 public class DemoTable extends AppCompatActivity implements View.OnTouchListener { ImageView rBall;

当特定的活动加载时,我的堆从16mb左右跳到64mb左右!起初,我认为这是由于我在drawable dir中调整了大png的大小造成的,但我已经将它们调整为更小的大小,因为它们只显示得很小

我不明白的是,当我退出活动时,为什么堆大小没有减少,希望有人能给我指出解决这个问题的正确方向

    public class DemoTable extends AppCompatActivity implements View.OnTouchListener {
    ImageView rBall;
    ImageView yBall;
    ImageView bBall;
    ImageView cBall;

    @Override
    protected void onStart() {
        super.onStart();
        rBall = (ImageView) findViewById(R.id.rBall);
        rBall.setOnTouchListener(this);

        yBall = (ImageView) findViewById(R.id.yBall);
        yBall.setOnTouchListener(this);

        bBall = (ImageView) findViewById(R.id.bBall);
        bBall.setOnTouchListener(this);

        cBall = (ImageView) findViewById(R.id.cBall);
        cBall.setOnTouchListener(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_table);


    }

    float x, y = 0.0f;
    boolean moving = false;

    @Override
    public boolean onTouch(View v, MotionEvent event) {


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                moving = true;
                break;
            case MotionEvent.ACTION_MOVE:
                if (moving) {
                    x = event.getRawX() - v.getWidth() / 2;
                    y = event.getRawY() - v.getHeight() * 3 / 2;
                    v.setX(x);
                    v.setY(y);
                }
                break;

            case MotionEvent.ACTION_UP:
                moving = true;
                break;
        }

        return true;
    }

    @Override
    protected void onDestroy() {

        rBall = null;
        yBall = null;
        bBall = null;
        cBall = null;
        System.gc();
        super.onDestroy();
    }
}
有4个rBall实例(11kb)、4个yBall实例(12kb)、1个cBall实例(11kb)和1个bBall实例(7kb) 下面是xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/table_demo"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/rBall"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/r_ball" />

        <ImageView
            android:id="@+id/rBall2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/r_ball" />

        <ImageView
            android:id="@+id/rBall3"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/r_ball" />

        <ImageView
            android:id="@+id/rBall4"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/r_ball" />

        <ImageView
            android:id="@+id/yBall"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/y_ball" />

        <ImageView
            android:id="@+id/yBall2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/y_ball" />

        <ImageView
            android:id="@+id/yBall3"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/y_ball" />

        <ImageView
            android:id="@+id/yBall4"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/y_ball" />

        <ImageView
            android:id="@+id/bBall"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/b_ball" />

        <ImageView
            android:id="@+id/cBall"
            android:layout_width="27dp"
            android:layout_height="27dp"
            android:src="@drawable/c_ball" />
    </LinearLayout>

</RelativeLayout>


这根本不应该是原因,但是,在方法onDestroy.moved中释放对球的引用之前,您正在调用System.gc(),当按back时,您的右侧,而不是原因仍然是63.44mb。我认为您不需要使用
android:src
,因为每个图像视图都加载了自己的可绘制内容,而不是同一个。好的,我做了更多的尝试,发现如果我使用android Studio的内置监视器按钮“Initiate GC”,它会将堆大小清除回15mb左右,那么为什么我的onDestroy()类中的system.GC()行没有被调用呢?实际上,调用
system.GC()的情况非常罕见
应手动完成。什么时候执行垃圾收集应该由VM决定,您不需要关心它。此外,如果您真的需要通过将ImageView成员设置为null来“释放”它们,我会感到惊讶。一般来说,Java在内存管理方面非常方便,在99%的情况下,您不需要关心这些东西。这根本不是原因,但是,在方法onDestroy.moved It中释放对球的引用之前,您正在调用System.gc(),不是因为按back时仍为63.44mb我认为您不需要使用android:src,因为每个图像视图都加载了自己的可绘制文件,而不是相同的文件。好的,我做了更多的尝试,发现如果我使用android Studio的内置监视器按钮“Initiate GC”,它会将堆大小清除回15mb左右,那么为什么我的onDestroy()类中的system.GC()行没有被调用呢?实际上,调用
system.GC()的情况非常罕见
应手动完成。什么时候执行垃圾收集应该由VM决定,您不需要关心它。此外,如果您真的需要通过将ImageView成员设置为null来“释放”它们,我会感到惊讶。一般来说,Java在内存管理方面非常方便,在99%的情况下,您不需要关心这些东西。