1. bitsquid
  2. foundation
  3. Issues
Issue #10 new

Delayed Destruction of Queue Elements

Ashkan Aliabadi
created an issue

This is probably by design, but unless I am misunderstanding the code, queue elements are not destructed when they are popped. Considering that they will eventually be destructed when the queue itself is destructed or the underlying array is resized, there is no memory leak in the strict sense of the word, but resources held by items stored in the queue can remain allocated potentially much longer than needed.

Consider the following use case: a hundred elements are added to the queue (for instance under heavy load in a task manager's queue), 95 of them are popped afterwards, and for the remainder of the application's lifetime, the queue size will not grow past 10. In this case, 90 resources will stay allocated for the entire duration of the application.

P.S. Thank you for sharing, both your code and thoughts (in your blog).

Comments (4)

  1. Christian Magnerfelt

    The queue uses an array internally which means whenever the the size of the queue exceeds the size of the array, the array will be expanded to hold the new elements. The array does not shrink if you remove elements. The benefit of this is that you can reserve space up front for the queue to avoid unnecessary allocation while using the queue. Storing the queue in an array is also good as the memory layout is contiguous. This makes it easier to shift the data in the queue when performing a push or pop at the front of the queue. But why? The main reason is that shifting the data may in some cases be faster rather than allocating new memory due to latency between the main memory and the CPU cache. It depends on the use case though so it's important to measure the load before drawing any conclusions.

    Your use case also seems strange to me. Hundreds of elements? How large are the elements? It does not really seem like a heavy load to me.

    /Criz

  2. Ashkan Aliabadi reporter

    Your answer does not pertain to the issue I brought up. Your queue analysis does not answer the concern that the resources held by array and queue elements can stay allocated longer than required. Still, I think that has been by design.

    P.S. The queue is a growable ring buffer. There is no shifting of the elements on front pushes.

  3. Log in to comment