试图将java对象存储在连续的内存中 [英] trying to store java objects in contiguous memory

查看:119
本文介绍了试图将java对象存储在连续的内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现类似缓存的对象集合。目的是通过内存中的位置快速访问这些对象,因为我可能一次只能读取多个对象。我目前只是将对象存储在像vector或deque这样的java集合对象中。但我不相信这会利用连续的内存。

I am trying to implement a cache-like collection of objects. The purpose is to have fast access to these objects through locality in memory since I'll likely be reading multiple objects at a time. I currently just store objects in a java collections object like vector or deque. But I do not believe this makes use of contiguous memory.

我知道这可以用C语言完成,但可以用Java完成吗?这些对象可能具有不同的长度(因为它们可能包含字符串)。有没有办法通过Java分配连续的内存?是否有一个Java集合对象?

I know this can be done in C, but can it be done in Java? These objects may be of varying lengths (since they may contain strings). Is there a way to allocate contiguous memory through Java? Is there a Java collections object that would?

请告诉我。

谢谢,
jbu

Thanks, jbu

推荐答案

你不能强迫它。如果快速连续分配所有对象,则它们可能是连续的 - 但如果将它们存储在集合中,则无法保证集合将是实际值的本地集合。 (该集合将引用对象,而不是包含对象本身。)

You can't force it. If you allocate all the objects in quick succession they're likely to be contiguous - but if you're storing them in a collection, there's no guarantee that the collection will be local to the actual values. (The collection will have references to the objects, rather than containing the objects themselves.)

此外,GC压缩将在内存中移动值。

In addition, GC compaction will move values around in memory.

您是否真的对自己的应用进行了分析,发现这是一个瓶颈?在大多数情况下,我希望其他优化措施能够以更可靠的方式帮助您。

Have you actually profiled your app and found this is a bottleneck? In most cases I'd expect other optimisations could help you in a more reliable way.

这篇关于试图将java对象存储在连续的内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆