In Java, a weak reference is garbage collected if memory out. In Linux, malloc()
always returns a strong reference, ie. the pointer is never freed until the caller call free()
function.
I want to allocate a buffer for caching, which could be freed automatically when the memory is running out, like following:
cache_t cache;
if (! cache_alloc(&cache))
die("Memory out");
cache_lock(&cache); // realloc cache mem if it is collected
if (! cache->user_init) { // The "user_init" maybe reset if the cache mem is collected
// lazy-init the cache...
load_contents(cache->mem, ...);
cache->user_init = 1;
}
// do with cache..
stuff_t *stuff = (stuff_t *) cache->mem;
...
cache_unlock(&cache);
It seems the buff
and cache
in the output of vmstat
is disk IO related:
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 51604 554220 13384 314852 3 10 411 420 702 1063 8 3 75 14
Well, I want to know more about whether the cache in my example could be reflected in the "cache" column in the vmstat output.