Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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
可能的android内存泄漏_Android_Memory_Memory Leaks - Fatal编程技术网

可能的android内存泄漏

可能的android内存泄漏,android,memory,memory-leaks,Android,Memory,Memory Leaks,我有这个安卓项目,不知道为什么它会吃掉RAM?注:只需填写RAM,无需处理。我读了很多书,但不知道该怎么办。我对android和java都是新手 刚刚注意到(在Android Monitor-RAM中)我的应用程序只使用了4MB的内存。 如果有人尝试运行(请执行-MainActivity是launcher,MyCanvas只是用作视图的一个类),我可以使用高达100的速度变量(甚至可能更多),在10毫秒内或更频繁地进行线程回调-这不会产生任何问题。但一旦我触摸并移动垃圾收集器,它就会暂停一切 M

我有这个安卓项目,不知道为什么它会吃掉RAM?注:只需填写RAM,无需处理。我读了很多书,但不知道该怎么办。我对android和java都是新手

刚刚注意到(在Android Monitor-RAM中)我的应用程序只使用了4MB的内存。 如果有人尝试运行(请执行-MainActivity是launcher,MyCanvas只是用作视图的一个类),我可以使用高达100的速度变量(甚至可能更多),在10毫秒内或更频繁地进行线程回调-这不会产生任何问题。但一旦我触摸并移动垃圾收集器,它就会暂停一切

MyCanvas.java(用作视图)

我有问题。 我试图在每次触摸事件发生时调用我的方法,这是我们无法做到的,因为当你拖动鼠标时,实际上会发生数千个(可能更多)触摸事件

试着从代码中理解。在主活动中,我们有一个onTouch事件侦听器,它调用changeGravity()方法,该方法依次调用calcDist()和setGravity()(从calcDist()开始)。因此产生了大量的垃圾,因此GC开始发挥作用,它暂停应用程序,一切看起来都挂起了


解决方案-只需将calcDist()调用放在线程内,那么不管重力值是多少,都将在10毫秒内生效。与从changeGravity()调用相比,我们的模拟器和手机能够做到这一点方法。

看起来您的
处理程序
MyView
中可能正在泄漏
MyView
类,由于您使用
Handler.postdayed
的方式,该类随后也会泄漏与其关联的
活动。尝试暂时注释掉
处理程序的用法,看看内存问题是否仍然存在(假设这是代码中唯一的泄漏)。如果不是,那么我们可以讨论一些可能的修复方法。而且,根据您的编辑,如果我的猜测是正确的,那么当您在纵向和横向模式之间来回旋转设备时,内存使用量应该会继续增加,从而导致每次创建托管自定义视图的活动的新实例。我使用什么来代替处理程序??。坦白地说,我不知道线程的工作原理,我使用它只是因为它做了我想要的。因为我需要不断地刷新视图。我想要的是像“粒子流”(Play Store上的搜索)这样的应用程序。您可以尝试使用静态处理程序,并对视图进行弱引用,如前所述。问题/答案是关于
服务中的
处理程序
,但它仍然适用于
视图中的处理程序
。或者,在活动暂停时,将
处理程序
移动到承载视图的
活动
片段
。您可以在活动恢复时添加回调。
package com.abc.mygraphics;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import java.util.Random;
import android.os.Handler;

public class MyCanvas extends View {

public static Paint paint;
//public static int n = 5000;
public static int N = 100, particleSize = 3;
public static int height, width;
public static float[][] pos = new float[N][2];
public static float[][] pos0 = new float[N][2];
//public static float distX, distY, dist;
Handler handler = new Handler();
public static int speed = 1;
public static int gravityX, gravityY;
public static Random random = new Random();
public static int[] colorList = new int[10];
public static int sphereSize = 100, M = 10;
public static float[] distance = new float[N];
public static int[][] gravity = new int[N][2];
public static boolean[] direction = new boolean[N];
public static int[] moved = new int[N];

public MyCanvas(Context context) {
    super(context);

    width = MainActivity.sharedPreferences.getInt("screenX", 200);
    height = MainActivity.sharedPreferences.getInt("screenY", 200);
    colorList = new int[]{Color.RED, 0xffff4000, 0xffffff00, 0xff40ff00, 0xff00ff00, 0xff00ff80, 0xff0080ff, 0xff0000ff, 0xffbf00ff, 0xffff0040};

    paint = new Paint();
    paint.setColor(Color.GRAY);
    int i;
    for (i = 0; i < N; i++) {
        pos[i][0] = random.nextInt(width);
        pos[i][1] = random.nextInt(height);
        pos0[i][0] = pos[i][0];
        pos0[i][1] = pos[i][1];
        moved[i] = 0;
        direction[i] = true;
    }

    final Runnable r = new Runnable() {
        public void run() {
            invalidate();
            handler.postDelayed(this, 100);
        }
    };
    handler.postDelayed(r, 100);
}

public static void changeGravity(){
    gravityX = MainActivity.sharedPreferences.getInt("x",200);
    gravityY =   MainActivity.sharedPreferences.getInt("y",200);
    // gravityY -= 68;
    for(int i=0;i<N;i++){
        calcDist(i);
    }
}

@Override
public void onDraw(Canvas canvas){
    int i;
    int x;  //remove at some poitnt
    canvas.drawColor(Color.BLACK);
    //canvas.drawCircle(gravityX,gravityY, 50, paint);

    for(i=0;i<N;i++) {
        setPosition(i);
        x =  random.nextInt(10);
        paint.setColor(colorList[x]);
        canvas.drawCircle(pos[i][0], pos[i][1], particleSize, paint);
    }
}

public static void setPosition(int i){
    // this is wrong
    if(moved[i] >= (int)distance[i]/speed){
        direction[i] = !direction[i];
        setGravity(i);
    }
    else{
        pos[i][0] = pos0[i][0] + speed*moved[i]*(gravity[i][0]- pos0[i][0])/distance[i];
        pos[i][1] = pos0[i][1] + speed*moved[i]*(gravity[i][1]- pos0[i][1])/distance[i];
        moved[i]++;
    }
}

public static void calcDist(int i){
    setGravity(i);
    distance[i] = (float) Math.sqrt( Math.pow((gravity[i][0]-pos[i][0]),2) +  Math.pow((gravity[i][1]-pos[i][1]),2) );
}

public static void setGravity(int i){

    // do not forget to set pos0 in both cases
    if(direction[i]){
        gravity[i][0] = gravityX;
        gravity[i][1] = gravityY;
    }

    else{
        // make this biased on radius of sphere
        gravity[i][0] =  random.nextInt(width);
        gravity[i][1]  = random.nextInt(height);
    }
    pos0[i][0] = pos[i][0];
    pos0[i][1]  = pos[i][1];
    moved[i]=0;
}



}
package com.abc.mygraphics;

 import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager; 
import android.widget.EditText;
import android.widget.TextView;

import com.abc.mygraphics.MyCanvas;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

//DrawingView dr = new DrawingView(this);

public  static SharedPreferences sharedPreferences;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;


    sharedPreferences = getSharedPreferences( getPackageName() + "_preferences", MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("screenX",width);
    editor.putInt("screenY", height);
    editor.putInt("x", 200);
    editor.putInt("y", 200);
    editor.commit();
    setContentView(new MyCanvas(this));
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // int x = (int)event.getX();
    //int y = (int)event.getY();
    int x = (int) event.getRawX();
    int y = (int) event.getRawY();
    //sharedPreferences = getSharedPreferences("CheckSharing",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("x", x);
    editor.putInt("y", y);
    editor.apply();
    MyCanvas.changeGravity();

    return false;
}

}