Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will this cause any memory leaks?

Tags:

java

android

i am fairly new to the java world and was wondering if the following would cause any memory leaks surrounding me reassigning up and down to null. just want to make sure that this wont cause any memory leaks because those are bad

import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;

public class TouchHandler {
    public TouchHandler() {

    }

    static Point down;
    static Point up;
    static boolean isUp = false;
    static boolean isDown = false;

    public static void processEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("betterinf", "ACTION DOWN");
                down = new Point();
                down.x = (int) event.getX();
                down.y = (int) event.getY();
                isDown = true;

                break;

            case MotionEvent.ACTION_UP:
                Log.i("betterinf", "ACTION UP");
                down = new Point();
                up.x = (int) event.getX();
                up.y = (int) event.getY();
                isUp = true;

                break;

            case MotionEvent.ACTION_MOVE:

                break;
        }
    }

    public static void Update(Long deltaTime) {
        if (isDown && isUp) {
            //event has happened
            isDown = false;
            isUp = false;
            down = null;
            up = null;

            Point vel = new Point();
            vel.x = down.x - up.x;
            vel.y = down.y - up.y;
            GM.getBallManager().newPlayerBall(down, vel);
        }

    }

}
like image 852
PandaTurtle Avatar asked Dec 30 '25 09:12

PandaTurtle


2 Answers

The garbage collector in this example will appropriately deal with any allocated memory no longer being used. So, no this will not give you a memory leak. It is important to note that you can still create memory leaks, and I suggest you look up how they can be done to avoid them in the future.

like image 166
Brandon Ling Avatar answered Jan 02 '26 01:01

Brandon Ling


Java is a garbage collected language, you will not get memory leaks. However that being said you can leak native resources so always close files, sockets, database cursors etc when finished with them.

like image 23
rhyshort Avatar answered Jan 02 '26 01:01

rhyshort