The problem is the following there is an application, in one of which Activiti there is a customView. With frequent changes of orientation with active activit with the view or with a change of activations back and forth - it turns out Out of memory.

Also in Android Monitor, it is clear that after 3 minutes the garbage collector sometimes still "deigns" to clear the memory, but sometimes it does not ... and 9 switches between this and other activations (back and forth) lead to crash of the application.

And all the graphics on the canvas is drawn, the pictures are not used. This is my first application on Android .. and maybe there is a lack of a method that kills all the objects on my View and calls it on onDestroy.

  • 2
    maybe not enough, but we don't know, because only you see the code. - Nikotin N
  • Activity is quite a large object, so when you switch between them - and you add a new one to the stack every time, right? You have filled RAM, which is allocated for the application. Try to ensure that at the moment as few large objects as possible are in memory, do not leave static references to them (a very common error). By the way, GC is called when memory filling is close to critical and only for objects that have no live links - are you sure that all links to your activity are dead? - iamthevoid

1 answer 1

In general, the solution was found, it was probably some kind of “bydlyattskoe” and it was necessary to look for why this “Memory Leak” occurs, but everything was decided very simply:

When we move to another activity:

Intent intent = new Intent(getApplicationContext(), TestActivity.class); startActivity(intent); finish(); System.exit(0); 

This miracle code clears the memory completely.

The problem was the same when changing the orientation, it went away after creating a method to disable the redraw stream in the view element.

  public void DestroyView() { removeCallbacks(animator); } private Runnable animator = new Runnable() { @Override public void run() { boolean needNewFrame = false; long now = AnimationUtils.currentAnimationTimeMillis(); for (Dynamics dynamics : datapoints) { dynamics.update(now); if (!dynamics.isAtRest()) { needNewFrame = true; } } for (Dynamics dynamics : devicespoints) { dynamics.update(now); if (!dynamics.isAtRest()) { needNewFrame = true; } } if (needNewFrame) { postDelayed(this, 15); } invalidate(); } }; }