I am writing a cache server in java which will cache image data (jpgs, pngs, tiff etc) in memory for immediate access over http. The pictures are made by another service, that is an costly operation, so I wish to cache them on my small cache server.
You will find the key reason why I am writing it on your own, therefore the answer I am searching for isn't [some clever software product]
Question: How do i keep certain some data objects in primary memory, and be sure that information is really in primary memory after i require it, and never pressed to disk with a virtual memory manager? That's, how do i do that in Java?
More information: Objects might be recommended with any interval, e.g. days or say years apart to become a little extreme :-)
EDIT: I've discovered this SO post which asks "are you able to keep objects in contiguous memory?" - This isn't the question I am asking, even though it may help, if objects were recommended constantly, I presume. And btw, the response to that question was "no", except clearly for value-types in arrays.
I strongly doubt this can be done in Java alone. You'll most likely need to use something similar to [cde] through JNI, along with the requisite JNI incantations to pin the cached objects graphs in memory therefore the GC does not move them. And [place miracle here] to compact the pinned memory into contiguous pages because that is what
mlock works on.
I suppose you need to keep access time predictably low would you like to avoid paging. In Java you've limited group of tool to handle memory. Actually, this really is operating systems' job to trace which pages are inactive and may be pressed to disc. I'm not even sure whether there's any API in main os's to manage this behavior.
That being stated, you have to concentrate on kidding the machine that pages are really needed, when they were not really employed for a very long time. I believe you know the solution - just write an asynchronous task that touches every object inside your cache every second approximately. This will result in the operating-system to think that you simply process continues to be positively with such regions of memory.
Sad but ought to be effective.