当进程被杀死时如何从内存中清除进程

发布于 2024-11-08 11:29:14 字数 133 浏览 0 评论 0原文

如何在进程被杀死时从内存中清除进程,我不在乎进程是空的还是什么都不做。只是想从内存中清除它。

或者

1M 文件读取一次后,读取两次时将从内存/缓存中读取。 问题是一次读取后可以从内存或缓存中清除吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-11-15 11:29:14

最好的答案是:你不“清除它”:你让 android 处理它们。如果(当)需要内存时,任何进程都会被终止。关于缓存,垃圾收集器将处理它。再说一遍,您无需做任何事情(也许除了确保您没有在某处留下对其的实时引用)。

我引用以下部分 Dianne Hackborn 的在 Android 博客中发布

应用程序何时“停止”?

一个常见的误解
Android 多任务处理就是区别
进程和应用程序之间。
在 Android 中,这些并不严格
耦合实体:应用程序可以
似乎没有任何信息就呈现在用户面前
当前运行的实际进程
应用程序;多个应用程序可以共享
流程,或一个应用程序可能使
使用多个进程取决于
它的需求;的过程
应用程序可能会被保存在
Android 即使该应用程序是
不积极做某事。

事实上,你可以看到
应用程序的进程“运行”确实
并不意味着应用程序正在运行或
做任何事。它可能只是在那里
因为 Android 在某些时候需要它
点,并决定
最好保留它以防万一
再次需要它。同样,你可以
留下一点申请
然后从离开的地方返回
关闭,在此期间 Android 可能会
需要摆脱这个过程
用于其他事情。

Android 如何处理的关键
这种方式的应用是
进程不会完全关闭。
当用户离开应用程序时,
它的过程保存在
背景,让它继续
工作(例如下载网页
页)如果需要,请立即来
如果用户返回到前台
到它。如果设备永远不会耗尽
内存,那么 Android 将保留所有
这些过程围绕着,真正离开
所有应用程序“运行”所有的
时间。

当然,数量有限
内存,并适应这个
Android 必须决定何时摆脱
不需要的流程。这
导致Android的进程生命周期,
它用来决定如何进行的规则
每个过程都很重要,因此
下一个应该被删除的。这些
规则基于重要性
一个进程是针对用户当前的
经验,以及已有多长时间
自从上次需要该过程以来
由用户。

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.

玻璃人 2024-11-15 11:29:14

如果您正在寻找终止该进程,这可能会有所帮助,

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文