Java and virtual memory: control / influence on objects stored in main memory?

I am writing a cache server in java that will cache image data (jpgs, pngs, tiff, etc.) in memory for quick access via http. Images are rendered by another service, which is an expensive operation, so I want to cache them on my cache server.

There are several reasons why I write this from scratch, so the answer I'm looking for is not [some smart software product]

Question How can I save a certain set of data objects in the main memory and make sure that the data is actually in the main memory when I need it and not push the disk to the virtual memory manager? That is, how can I do this in Java?

Additional Information: Objects can be referenced at any interval, for example. days or say years apart to be a little extreme :-)

EDIT : I found this SO post that asks: "Can you save objects in continuous memory?" β€œThis is not a question that I am asking about, although it could help if the objects were referenced all the time, I suppose.” And by the way, the answer to this question was β€œno”, except, obviously, for value types in arrays.

+4
source share
2 answers

I very much doubt that you can only do this in Java. You will probably have to use something like mlock via JNI, as well as the necessary JNI spells to bind graphs of cached objects in memory, so GC does not move them. And [insert the miracle here] to compact the pinned memory into adjacent pages, because mlock works.

+1
source

I assume that you want access times to be predictably low, so you want to avoid paging. In Java, you have a very limited set of tools for managing memory. In fact, it is the work of operating systems to track which pages are inactive and can be transferred to disk. I'm not even sure if there is any API in the main operating systems to control this behavior.

In doing so, you should focus on fooling the system that you actually need pages, while they have not been used for a long time. I think you already know the answer - just write an asynchronous task that affects every object in the cache every second or so. This should make the operating system believe that you are still actively using these areas of memory.

Sad, but must be effective.

+1
source

Source: https://habr.com/ru/post/1392377/


All Articles