3

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);
        }

    }

}
  • 2
    The garbage collector will handle this appropriately, so no. – Brandon Ling Jul 15 '15 at 20:28
  • possible duplicate of [What is the garbage collector in Java?](http://stackoverflow.com/questions/3798424/what-is-the-garbage-collector-in-java) – Turing85 Jul 15 '15 at 20:28
  • @BrandonLing pls post as answer – PandaTurtle Jul 15 '15 at 20:30
  • I think that a simple yes/no question and answer won't help you understand how memory leaks can or cannot happen. Why don't you tell us which lines in your program you suspect could be problematic and why. That way, you'll get a much more valuable answer. Otherwise, you'll move on to your next program, and will keep wondering if a memory leak is possible. – sstan Jul 15 '15 at 20:31
  • @PandaTurtle As requested, I posted an answer – Brandon Ling Jul 15 '15 at 20:35
  • @PandaTurtle : In your update() method you have `GM.getBallManager().newPlayerBall(down, vel);` - what exactly is `GM`, i.e., what sort of class does it extend (if any)? – Squonk Jul 15 '15 at 20:50

4 Answers4

2

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.

Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
1

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.

rhyshort
  • 1,453
  • 11
  • 17
1

Fear not, the garbage collector will handle this!

I think just mentioning garbage collector and then bla bla bla will not going to help you in the fullest sense.

As Java has GB so, creating a true memory leak in Java is not so easy! There are a lot fo Q/A in SO like this: Creating a memory leak with Java. You can read this article too where memory leaks described with nice examples.

The following things may occur memory leaks in java:

  • If the number of file handler exceeds (e.g. opening file in/out stream many times without closing it)

  • If any static field holds object reference.

  • Any Native methods that locks memory is out of the scope of JVM garbage collector. So that can cause memory leaks.

Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
0

Java has a garbage collector. Actually, I saw a error in your code:

 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;

modify

 down = new Point();

to

 up = new Point();