Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:45:01+00:00 2026-06-11T22:45:01+00:00

In Java, a weak reference is garbage collected if memory out. In Linux, malloc()

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:45:02+00:00Added an answer on June 11, 2026 at 10:45 pm

    There really isn’t a good way of doing it – the C memory model simply doesn’t allow for the same kind of behavior that the Java memory model allows. Java’s memory model of course builds on the C model when interfacing with the operating system, which is why the Java heap must be manually limited by the application launcher.

    The “buff” and “cache” columns relate to the page/disk cache and internal buffers used by the kernel. These caches are automatically handled by the kernel – for instance, reading a file will place the contents in the “cache” usage number, in the same way that running out of memory will commit it to a swap device (“swpd”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why are Java weak references not counted as references with respect to garbage collection?
What is the proper approach to testing a weak reference in Java? My initial
Can Locks (java.util.concurrent.locks.Lock) be garbage collected while locked? Suppose a purely theoretical example: WeakReference
I am looking for a weak reference implementation similar to java.lang.ref.WeakReference , but which
There are four type of references in Java: Strong, Soft, Weak, and phantom. Kindly
I come from lots of Java programming and weak at JavaScript.I always used component
Effective Java says: A third common source of memory leaks is listeners and other
I'm looking for things like Dynamic typing, Static Typing, Weak Typing, and Strong Typing.
I wonder how weak references work internally, for example in .NET or in Java.
Is there any way to find out the garbage collector of javascript destroyed the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.