简体   繁体   English

Akka actor 消息需要内存池

[英]Akka actor message needs memory pool

I a new in java.我是java新手。 I'm c++ programmer and nowadays study java for 2 months.我是 C++ 程序员,现在学习了 2 个月的 Java。 Sorry for my pool English.对不起,我的游泳池英语。

I have a question that if it needs memory pool or object pool for Akka actor model.我有一个问题,它是否需要 Akka 演员模型的内存池或对象池。 I think if i send some message from one actor to one of the other actors, i have to allocate some heap memory(just like new Some String, or new Some BigInteger and other more..) And times on, the garbage collector will be got started(I'm not sure if it would be started) and it makes my application calculate slowly.我想如果我从一个演员向其他演员之一发送一些消息,我必须分配一些堆内存(就像新的 Some String,或新的 Some BigInteger 等等..)而且时间长了,垃圾收集器将开始了(我不确定它是否会启动)并且它使我的应用程序计算缓慢。

So I search for the way to make the memory-pool and failed(Java not supported memory pool).所以我寻找制作内存池的方法并失败了(Java不支持内存池)。 And I Could Make the object pool but in others project i did not find anybody use the object-pool with actor(also in Akka Homepage).我可以创建对象池,但在其他项目中,我没有发现任何人将对象池与演员一起使用(也在 Akka 主页中)。

Is there any documents bout this topic in the akka hompage?在 akka 主页中有关于这个主题的任何文件吗? Plz tell me the link or tell me the solution of my question.请告诉我链接或告诉我我的问题的解决方案。

Thanks.谢谢。

If, as it's likely you will, you are using Akka across multiple computers, messages are serialized on the wire and sent to the other instance.如果您可能会在多台计算机上使用 Akka,则消息会在线路上序列化并发送到另一个实例。 This means that simply a local memory pool won't suffice.这意味着仅仅本地内存池是不够的。

While it's technically possible that you write a custom JSerializer (see the doc here ) implementation that stores local messages in a memory pool after deserializing them, I feel that's a bit of an overkill for most applications (and easy to cock-up and actually worsen performance with lookup times in the map)虽然从技术上讲,您可以编写一个自定义 JSerializer(请参阅此处的文档)实现,在反序列化它们之后将本地消息存储在内存池中,但我觉得这对于大多数应用程序来说有点过头了(而且很容易搞砸并实际上变得更糟)性能与地图中的查找时间)

Yes, when the GC kicks in, the app will lag a bit under heavy loads.是的,当 GC 启动时,应用程序在重负载下会有点滞后。 But in 95% of the scenarios, especially under a performant framework like Akka, GC will not be your bottleneck: IO will.但是在 95% 的场景下,尤其是在像 Akka 这样的高性能框架下,GC 不会成为你的瓶颈:IO 会。

I'm not saying you shouldn't do it.我不是说你不应该这样做。 I'm saying that before you take on the task, given its non-triviality, you should measure the impact of GC on your app at runtime with things like Kamon or other Akka-specialized monitoring solutions, and only after you are sure it's worth it you can go for it.我的意思是,在你接受这项任务之前,考虑到它的非平凡性,你应该在运行时使用 Kamon 或其他 Akka 专用监控解决方案之类的东西来衡量GC 对你的应用程序的影响,并且只有在你确定它是值得的之后你可以去争取。

Using an ArrayBlockingQueue to hold a pool of your objects should help,使用 ArrayBlockingQueue 来保存对象池应该会有所帮助,

Here is the example code.这是示例代码。

TO create a pool and insert an instance of pooled object in it.创建一个池并在其中插入池对象的实例。

BlockingQueue<YOURCLASS> queue = new ArrayBlockingQueue<YOURCLASS>(256);//Adjust 256 to your desired count. ArrayBlockingQueues size cannot be adjusted once it is initialized.


queue.put(YOUROBJ); //This should be in your code that instanciates the pool

and later where you need it (in your actor that receives message)以及稍后您需要它的地方(在您接收消息的演员中)

YOURCLASS instanceName = queue.take();

You might have to write some code around this to create and manage the pool.您可能需要围绕此编写一些代码来创建和管理池。 But this is the gist of it.但这是它的要点。

One can do object pooling to minimise long tail of latency (by sacrifice of median in multythreaded environment).可以进行对象池以最小化延迟的长尾(通过牺牲多线程环境中的中位数)。 consider using appropriate queues eg from JCTools, Distruptor, or Agrona.考虑使用适当的队列,例如来自 JCTools、Distruptor 或 Agrona 的队列。 Don't forget about rules of engagement for state exhange via mutable state using multiple thereads in stored objects - https://youtu.be/nhYIEqt-jvY (the best content I was able to find).不要忘记使用存储对象中的多个读取通过可变状态进行状态交换的参与规则 - https://youtu.be/nhYIEqt-jvY (我能找到的最佳内容)。

Again, don't expect to improve throughout using such slightly dangerous techniques.同样,不要指望使用这种稍微危险的技术来提高整个过程。 You will loose L1-L3 cache efficiency and will polite PCI with barriers.您将失去 L1-L3 缓存效率,并会礼貌地使用障碍物 PCI。

Bit of tangent (to get sense of low latency technology): One may consider some GC implementation with lower latency if you want to stick with Akka, or use custom reactive model where object pool is used by single thread, or memory copied over eg Distrupptor's approach.切线点(以了解低延迟技术):如果您想坚持使用 Akka,或者使用自定义反应模型,其中对象池由单线程使用,或者通过例如 Distrupptor 的内存复制,可以考虑一些延迟较低的 GC 实现方法。 Another alternative is using memory regions (the way Erlang VM works).另一种选择是使用内存区域(Erlang VM 的工作方式)。 It creates garbage, but in form easy to handle by GC!它会产生垃圾,但形式很容易被 GC 处理!

If you go to very low latency IO and are the biggest enemy of latency - forget legacy TCP (vs RDMA over Infininiband), switches (over swichless), OS accessing disk via OS calls and file system (use RDMA), forget interrupts shared by same core, not pinned cores (and without spinning for input) to real CPU core (vs virtual/hyperthreads) or inter NUMA communication or messages one by one instead of hardware multicast (or better optical switch) for multiple consumers and don't forget turning Epsilon GC for JVM ;)如果您使用非常低的延迟 IO 并且是延迟的最大敌人 - 忘记传统 TCP(与 RDMA over Infininiband)、交换机(通过 swichless)、OS 通过 OS 调用和文件系统访问磁盘(使用 RDMA),忘记共享的中断相同的核心,而不是固定核心(并且没有旋转输入)到真实的 CPU 核心(与虚拟/超线程)或 NUMA 间通信或消息一个接一个,而不是多个消费者的硬件多播(或更好的光开关),不要忘记为 JVM 转换 Epsilon GC ;)

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

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