trying to store java objects in contiguous memory

后端 未结 4 1460
情歌与酒
情歌与酒 2021-02-08 18:33

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 multip

4条回答
  •  醉话见心
    2021-02-08 19:24

    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.)

    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.

提交回复
热议问题