简体   繁体   中英

how to clear process from memroy when it is killed

how to clear process from memory when it is killed, i do not care the process is empty or do nothing. just want to clear it from memory.

OR

After read a 1M file once, it will be read from memory/cache when read it twice. the question is can it be clear from memory or cache after once read?

thanks

The best answer is: you don't "clear it" : you let android handle them. Any process will be killed if (when) there is a memory need. Regarding caches, the garbage collector will take care of it. Again, no need to do anything on your side (except, maybe, making sure you did not leave live references to it somewhere).

I am quoting below part of Dianne Hackborn's post in the android blog:

When does an application "stop"?

A common misunderstanding about Android multitasking is the difference between a process and an application. In Android these are not tightly coupled entities: applications may seem present to the user without an actual process currently running the app; multiple applications may share processes, or one application may make use of multiple processes depending on its needs; the process(es) of an application may be kept around by Android even when that application is not actively doing something.

The fact that you can see an application's process "running" does not mean the application is running or doing anything. It may simply be there because Android needed it at some point, and has decided that it would be best to keep it around in case it needs it again. Likewise, you may leave an application for a little bit and return to it from where you left off, and during that time Android may have needed to get rid of the process for other things.

A key to how Android handles applications in this way is that processes don't shut down cleanly. When the user leaves an application, its process is kept around in the background, allowing it to continue working (for example downloading web pages) if needed, and come immediately to the foreground if the user returns to it. If a device never runs out of memory, then Android will keep all of these processes around, truly leaving all applications "running" all of the time.

Of course, there is a limited amount of memory, and to accommodate this Android must decide when to get rid of processes that are not needed. This leads to Android's process lifecycle, the rules it uses to decide how important each process is and thus the next one that should be dropped. These rules are based on both how important a process is for the user's current experience, as well as how long it has been since the process was last needed by the user.

If you are looking for killing the process, this might help,

 int pid = android.os.Process.myPid();
     android.os.Process.killProcess(pid);
  Runtime r = Runtime.getRuntime();

    r.gc();
    System.gc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM