英文:
How is LMAX Disruptor garbage collector friendly
问题
我正在尝试理解lmax disruptor如何友好地处理垃圾回收。我看到环形上的事件对象被重新使用,然而,附加在这些对象上的“数据”是在堆上的,所以感觉垃圾回收的好处仅减少了每个事件的1个对象。如果“数据”是原始数据类型,那么在堆上就没有额外的内容,因此在这种情况下,好处是非常明显的。每个事件减少1个对象对于垃圾回收来说是否重要,或者这方面的情况比我理解的更复杂。
这篇文章http://mechanitis.blogspot.com/2011/06/dissecting-disruptor-whats-so-special.html表示“垃圾收集器在这里几乎没有什么事情要做” - 这对我来说不太明显,因此有了这个问题。
英文:
I am trying to understand how lmax disruptor is GC friendly. I see that the event objects on the ring are re-used, however, the "data" that is "set" on these objects are on the heap so it feels like the garbage collection benefit is limited to 1 object less per event. if the 'data' is of primitive data type then there's nothing additional on the heap, so the benefit in this case is very clear. Is 1 object less to GC per event significant or there's more to this than I understand w.r.t GC.
This article http://mechanitis.blogspot.com/2011/06/dissecting-disruptor-whats-so-special.html says the garbage collector has pretty much nothing to do here - this isnt very obvious to me, hence the question.
答案1
得分: 1
环形缓冲作为对象池的作用是 - 当你申请一个序列时,你可以将数据复制到事件中,当所有事件处理程序完成处理事件后,事件将再次返回到环形缓冲区。
关键在于确保将数据复制到事件中,因此事件能够容纳所有可能在生产者和消费者之间进行通信的不同类型的数据。如果你只是在生产者端在堆上分配一个对象,并将对这个对象的引用传递给事件,那么显然无法减少垃圾回收对内存的压力。
另一种方法是,有一个事件,它只是某些非堆内存的偏移量,可以通过使用飞行权重将其解释为不同类型的对象。
另一种方法 是拥有一个事件,它只是指向一些非堆内存的偏移量,可以通过使用飞行权重将其解释为不同类型的对象。
英文:
The ring buffer acts as a pool of objects - when you claim a sequence you can copy data into the event and when all the event handlers have finished processing the event will be returned to the ring buffer again.
The trick is make sure that you copy the data into the event and therefore that the event can hold all the different types of data you might want to communicate between the producers and consumers. If you simply allocate an object on the heap on the producer side and pass a reference to this object to the event then you will obviously not reduce the memory pressure on the GC at all.
Another approach is to have an event which is simply an offset into some off-heap memory which can be interpreted as different types of objects by using flyweights.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论