简体   繁体   中英

Java queue with memory size limit

I have been trying to find a Java queue implementation with a special size limit. In contrast to the usual size parameter that limits the count of objects in the queue, I need to limit the queue's maximum memory usage (in bytes). The queue would be used to hold objects of very different sizes, some huge and some very small. Ie the queue would hold only small amount of large objects and bigger amount of smaller objects.

Another possibility would be a queue with a size parameter in insert methods. When adding a new element to the queue I could specify how big the object to be added is.

Would anyone know of such Java queue implementation?

Thanks!

https://github.com/ehcache/sizeof

<dependency>
  <groupId>org.ehcache</groupId>
  <artifactId>sizeof</artifactId>
  <version>0.3.0</version>
</dependency>

This library gives you a way of checking the size of your queues:

private SizeOf sizeOf = SizeOf.newInstance();
sizeOf.deepSizeOf(someQueue)

In the add method of your implementation class always check the size of the queue using sizeOf.deepSizeOf and if this value exceeds your expected limit throw an IllegalStateException or handle the error otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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