简体   繁体   English

如何清除内存中被杀死的进程

[英]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. 一次读取1M文件后,两次读取将从内存/高速缓存中读取该文件。 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. 最好的答案是: 您不会“清除它” :让android处理它们。 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: 我在android博客中引用Dianne Hackborn的帖子的一部分:

When does an application "stop"? 应用程序什么时候“停止”?

A common misunderstanding about Android multitasking is the difference between a process and an application. 关于Android多任务处理的一个常见误解是流程和应用程序之间的差异。 In Android these are not tightly coupled entities: applications may seem present to the user without an actual process currently running the app; 在Android中,这些不是紧密耦合的实体:对于用户而言,似乎没有当前正在运行该应用程序的实际进程的应用程序; 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. 即使该应用程序没有积极地做某事,该应用程序的进程也可能会被Android保留。

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. 它可能只是存在,因为Android在某个时候需要它,并决定最好保留它,以防再次需要它。 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. 同样,您可能需要离开应用程序一小段时间,然后从上次中断的地方返回该应用程序,在此期间,Android可能需要摆脱其他原因的处理。

A key to how Android handles applications in this way is that processes don't shut down cleanly. Android如何以这种方式处理应用程序的一个关键是流程不会被彻底关闭。 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. 如果设备永远不会用完内存,那么Android将保留所有这些进程,真正使所有应用程序始终“运行”。

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. 当然,内存是有限的,要适应这种情况,Android必须决定何时清除不需要的进程。 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. 这导致了Android的流程生命周期,即用于确定每个流程的重要性的规则,从而决定了应该删除的下一个流程。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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