[英]Will my singleton be killed?
I have an application where I create a foreground service to make sure a user initiated upload continues even if the user quits the app or app is killed by memory pressure. 我有一个应用程序,在该应用程序中我创建了前台服务,以确保即使用户退出应用程序或由于内存不足而终止应用程序,用户启动的上传操作仍继续进行。
Both the service and app use a singleton to pass data around. 服务和应用程序都使用单例来传递数据。 I know this is not the best solution, so kindly ask to refrain from comments about bad-bad singletons as the question is not in this but in the following: 我知道这不是最好的解决方案,因此请不要发表有关坏单身人士的评论,因为问题不在此,而在以下内容中:
When I close the app, my foreground service continues running and can access the singleton. 当我关闭应用程序时,我的前台服务将继续运行并可以访问单例。 Is there a risk of the singleton being garbage collected due to memory pressure or are there any other memory related risks of using a singleton in this case? 是否存在由于内存压力而导致垃圾回收垃圾的风险,或者在这种情况下是否还有其他与内存相关的风险?
Service
will be relaunched later. 但是您的Service
将在稍后重新启动。 Static variables are only destroyed when the application is killed. 静态变量仅在终止应用程序时销毁。 So they won't just randomly disappear while your service is running. 因此,它们不仅会在您的服务运行时随机消失。 However if your entire service restarts you do have to account for the possibility of not having the singleton when it restarts. 但是,如果整个服务重新启动,则必须考虑重新启动时没有单例的可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.