Scala-Actors,避免内存泄漏是一个好习惯吗?
一切都始于问题“Scala,Actors,未读收件箱消息会发生什么? ”。我在想如何在具有许多参与者的大型系统中避免此类问题。
我发现自己写了这样的东西:
react {
//all cases
case any: AnyRef => logMessageWithoutCase(any)
}
它可以很好地避免内存泄漏还是有一些副作用?
更新1 感谢@Alexey Romanov 和@Luigi Plinge,系统中是否会有一些垃圾邮件演员?
像这样的:
react{
//all cases
case msg: Any => Spam!msg
}
最后在垃圾邮件中将记录或保存到数据库。我认为,这是更直观的解决方案。
All started with question "Scala, Actors, what happens to unread inbox messages?". I was thinking how to avoid such problems in large system with many actors.
I found myself writing something like this:
react {
//all cases
case any: AnyRef => logMessageWithoutCase(any)
}
Is it good avoidance from memory leaks or is it have some side effects?
UPDATE 1 Thanks to @Alexey Romanov and @Luigi Plinge, if in the system will have some Spam actor?
Something like this:
react{
//all cases
case msg: Any => Spam!msg
}
And finally in Spam will log or save to database. I think, it is more intuitive solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用 Akka Actor 进行调查,它们不会遇到此问题,因为它们强制按顺序进行消息处理。在这里,未处理的消息被传递给
unhandled()
回调,默认情况下该回调会记录并引发异常。另一件需要考虑的事情是,Akka actor 将在中期内取代当前的 scala.actor 包。这对于具有许多 Actor 的大型系统尤其有利,因为当前的 Scala Actor 不像 Akka Actor 那样轻量级。
You might also investigate using Akka actors, which do not suffer from this problem because they enforce in-sequence message processing. Here, unhandled messages get passed to the
unhandled()
callback, which by default logs and throws an exception.Another thing to consider is that Akka actors will replace the current scala.actor package in the medium term. This will be especially beneficial for large systems with many actors, because the current Scala actors are not as light-weight as Akka actors.
记录消息是一个副作用:)由于日志记录可能需要写入磁盘或数据库,因此如果您有许多不匹配的消息,这可能会降低性能。否则,是的,这是避免内存泄漏的好方法。
Logging messages is a side effect :) Since logging likely needs to write to disk or database, if you have many unmatched messages, this may degrade performance. Otherwise yes, this is a good way to avoid memory leaks.