为啥不会容易被回收 ,跟底层有什么关联?

解决方案 »

  1.   

    这个就和windows的后台服务是一样的,有的是开机就启动的,有的是当你打开某个程序时才启动,你想回收的话就在activity中的ondestroy中回收
      

  2.   

    这个service 既不是线程 也不是一个进程 如何实现的呢
      

  3.   

    和win32上的NT Service一样,只不过声明周期遵循android的java vm gc原则,只是并不是容易被结束掉。
      

  4.   

    恩恩,在android上gc的工作机制跟普通的JAVA程序滴GC回收机制是不通滴。在Java中如果一个对象不被引用了,那么会在GC时被回收。但是在android中 gc 会优先回收那些看不到的 activity ,至于先后顺序也是随机的。所以一个activity从前台转入了后台后,就意味着在任何时候都有可能被gc finish掉。如果一个process 里面没有可见的activity 也会被GC回收。但service 不一样, service 虽然也看不到,但是它是在后台工作的,所以gc 不会随便回收service ,service 的优先级比 activity要高。 至于gc 何时回收service,将遵循以下原则。1 the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed.2 If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible. Because only a few processes are generally visible to the user, this means that the service should not be killed except in extreme low memory conditions.3 If there are clients bound to the service, then the service's hosting process is never less important than the most important client. That is, if one of its clients is visible to the user, then the service itself is considered to be visible. 
      

  5.   

    和win32上的NT Service一样,只不过声明周期遵循android的java vm gc原则,只是并不是容易被结束掉。
    这三个 差不多就是我想要的答案了 
      

  6.   

    是的,有些是开机就启动了在systenserver添加进去,Zygote 之后开启android的service,在INIT.RC进行相关的属性配置。另外的就是程序中开起,可回收它~