简体   繁体   English

具有内存大小限制的 Java 队列

[英]Java queue with memory size limit

I have been trying to find a Java queue implementation with a special size limit.我一直在尝试寻找具有特殊大小限制的Java队列实现。 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?有人知道这样的Java队列实现吗?

Thanks!谢谢!

https://github.com/ehcache/sizeof 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.在您的实现类的add方法中,始终使用sizeOf.deepSizeOf检查队列的大小,如果此值超出您的预期限制,则抛出 IllegalStateException 或以其他方式处理错误。

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

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