1

I am new to android and stuck up with an issue.

I want to know how to force close the application programmatically and restart it again. I am trying this because i am really stuck up with OutOfMemmoryError i want to free up the memory and restart the app again.

Please guide me if i am wrong.

Macarse
  • 91,829
  • 44
  • 175
  • 230
Sat
  • 1,616
  • 3
  • 22
  • 40

4 Answers4

11

To force closing application just call:

android.os.Process.killProcess(android.os.Process.myPid());

About OutOfMemoryError - best place to handle this kind of errors is method Application.onLowMemory(), like:

@Override
public void onLowMemory ()
{
    super.onLowMemory();
    //do smth like freeing reources, etc.
}
Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • onLowMemory() is called only when the entire phone is running low in resource. It is not applicable for your App alone. http://developer.android.com/reference/android/content/ComponentCallbacks.html#onLowMemory() – AjOnFire Oct 24 '11 at 07:22
5

Restarting the app to avoid or repair OutOfMemoryExceptions is a very bad idea. You should start to work on your memory issue itself instead of just do weird things when they occur.

You should heal the disease instead of medicate the symptoms.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
2

I don't know if it's possible to force close manually but it's quite wrong.

Try checking your code and find out where the OutOfMemoryException is. Also check this question for guidance.

Community
  • 1
  • 1
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • I used this solution already..but my application is related to multiple images and this error is occurring only in some conditions... – Sat Dec 20 '10 at 11:54
0

for out of memory set attribute in android manifest android:largeHeap="true" with in application tag

Avinash Jadaun
  • 1,892
  • 17
  • 16