What are "weak references"? When to use them?

13

In managed memory environments such as Java, Python, and JavaScript, I've read something about weak references (% with%). I read that it had something to do with detecting the objects that can be collected, but it was not clear.

My questions are:

  • What is a weak reference ?
  • Is there a strong reference ?
  • When should I use it?
asked by anonymous 14.07.2018 / 04:03

2 answers

8

The typical and common references between objects are strong: If object A refers to object B by means of a strong reference, then object B can only be collected as garbage if A is also being.

With the weak references, the operation is different: If object A refers to object B through a weak reference, then object B can be collected as garbage even if object A can not be.

The purpose in using weak references is basically one: cache. For example, imagine that in your app you show several photos that are read from files and that you are often showing the same images. It happens that reading from a file repeatedly over and over is time-consuming and inefficient because the access to the disk is slow. Because of this, you should keep all these pictures in memory, avoiding rereading them from the disc. However, by keeping these pictures all in memory, you may end up consuming them completely, stifling other running processes that require memory. That way, you'll need some way to balance that, where photographs are discarded from memory when they might be disrupting other processes.

That's when weak references come in. If you keep the images in memory through weak references, they will stay there until they are disturbing other processes. As soon as the garbage collector throws an out-of-memory , it will try to clear some weak references to free up enough memory. In this case, your application will throw away some of the stored images, which will need to be reread from the disk if they need to be accessed again.

The result is that your application will strike a balance where you can automatically perform performance swaps, managed by the garbage collector. It will consume more memory for better performance and sacrifice performance when you need to save memory.

    
14.07.2018 / 15:59
0

Victor's answer explains what are weak and 'strong' references, but I've never heard one about the cache with respect to weak references.

The reason I use weak references in C ++ has to do with collection and reference cycles. For example, if A has a reference to B and B has a reference to A , the program can never collect the memory of A nor B , because the two refer. / p>

If B has a weak reference to A , the program can collect A when it no longer has 'strong' references. You can then collect B because the 'strong' reference within A no longer exists.

This type of reference cycle occurs within structures as double-linked lists.

C ++ does not have a powerful collector and std::shared_ptr does not resolve circular references.

    
15.07.2018 / 07:06