Soft (not: weak) C ++ links - is this possible? Is there an implementation?

In C ++, I use boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know this work with reference counting.

In Java, the memory is managed by the garbage collector, which considers embedded object references as strong, WeakReference weak and SoftReference as something intermediate (can be collected by GC, but can also survive in GC), which is very convenient for caching objects for some time, but discarding them as soon as free memory becomes low.

So now I am back in C ++, and I miss the fact that I have soft links. I wonder if soft handling with reference to an account is possible. When is the last strong link to the object cleared, and does the soft link remain when it is finally deleted? I could think of some schemes, but none of them seem clever to me.

Just in case, there is the correct semantics for soft links along with link counting, I wonder if this has already been implemented, perhaps in a way that is even compatible with boost::shared_ptr (or the C ++ TR1 equivalent of std::shared_ptr , if that gone).

If there is no answer to both questions, what are the alternatives to the object caching script?

EDIT: Of course, I'm talking about a situation where caching is really useful, because objects are expensive to create (think of a few database accesses and network requests), but there are too many of them to keep them forever.

+6
c ++ memory-management boost shared-ptr soft-references
Jul 04 '10 at 14:45
source share
5 answers

As others pointed out, you can find links to counted pointers (and their accompanying weak counterparts ) in the Boost library, but what is missing from the soft reference idea is some awareness of the limitations of the runtime memory environment. In Java, for example, SoftReference is not significantly different from WeakReference in its capabilities; rather, a contract on how the runtime will save or render two kinds of links, as opposed to memory pressure, which is different.

To simulate this behavior in C ++, you will need to create a cache that supports memory, which would contain strong references to objects that the rest of your application would hold weakly. When the cache determined that the application scratched its memory usage ceiling or some other restraining criteria, it would release strong links by passing objects for the โ€œcollectionโ€ (reaching zero links) and allowing the use of weak links to later detect invalidation.

+7
Jul 04 '10 at 18:15
source share
โ€” -

If you really want to reproduce this behavior, you can use the garbage collector (for example: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ ) and use it to take care of your object or its subset, where useful use SoftReferences.

But I would prefer that the solution be more native to C ++ than Java bahavior replication, but nothing prevents you from doing this.

+2
Jul 05 2018-10-10T00: 00Z
source share

You can implement your own LRU cache and the new smart_pointer associated with such a cache. I do not think that such a structure exists in Boost or standard C ++ (one way or another). If you are doing a web application or something else ... you can use libmemcached, which is the C interface for memcached.

Itโ€™s hard for me to think about a situation where such an object would be expensive and build / destroy ... while it would be cheap to reinitialize ... that the LRU cache would be useful. But if you really need it, you have the tools to create it.

+1
Jul 04 '10 at 17:52
source share

You can move your data with a soft link outside the application in the OS using something like buffcacher .

I don't know a single library offering this, I only ever rolled back my own.

Its so fast and fair that it is useful to cache the "secure cookie" check in web servers and other tasks that seem almost small for a regular cache.

0
Sep 27 '11 at 9:52
source share

No, in C ++ there is no such thing. Also should not be. Each object serves an important purpose. If it is not, why do you still have it? Thus, storing objects is a memory leak. If you need an object, you need it. If you do not, destroy it. Between the useful and the useless between them there is no gap between them, either it serves the purpose or not.

If you are desperate, it is impossible to write your own garbage collector and implement such a thing yourself. But I would recommend not using them or using them at all.

Edit: When caching objects, people usually use LRU caches. When the cache is skipped, the link to the least recently used object is destroyed (if the cache is full), a new object is created and placed as the last time, and all the rest are moved down. However, you usually need to extract items from disk before you actually need a C ++ caching strategy. The cost of creating most objects is simply not so high.

-2
Jul 04 '10 at 15:01
source share



All Articles