dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Reference Count

  1. #1
    Join Date
    Sep 2015
    Posts
    63

    Reference Count

    from the book "Professional Javascript for Web Developers 3rd edition" :

    -------------

    REFERENCE COUNTING

    A second, less-popular type of garbage collection is reference counting. The idea is that every value keeps track of how many references are made to it. When a variable is declared and a reference value is assigned, the reference count is one. If another variable is then assigned to the same value, the reference count is incremented. Likewise, if a variable with a reference to that value is overwritten with another value, then the reference count is decremented. When the reference count of a value reaches zero, there is no way to reach that value and it is safe to reclaim the associated memory. The garbage collector frees the memory for values with a reference count of zero the next time it runs.

    Reference counting was initially used by Netscape Navigator 3.0 and was immediately met with a serious issue: circular references. A circular reference occurs when object A has a pointer to object B and object B has a reference to object A, such as in the following example:

    Code:
    function problem(){
    	var objectA = new Object();
    	var objectB = new Object();
    
    	objectA.someOtherObject = objectB;
    	objectB.anotherObject = objectA;
    }
    In this example, objectA and objectB reference each other through their properties, meaning that each has a reference count of two. In a mark-and-sweep system, this wouldn’t be a problem because both objects go out of scope after the function has completed. In a reference-counting system, though, objectA and objectB will continue to exist after the function has exited, because their reference counts will never reach zero. If this function were called repeatedly, it would lead to a large amount of memory never being reclaimed. For this reason, Netscape abandoned a reference- counting garbage-collection routine in favor of a mark-and-sweep implementation in version 4.0.

    -------------

    I don't see how objetA and objectB wouldn't continue to exist if the circular reference wouldn't be there. In other words, taking the circular reference out of the equation, in what moment objectA and objectB would reach a reference count of zero?

    Thanks in advance!
    Bruno.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,426
    We have have recently the same query crop up and despite repeately stating the facts, the OP still couldn't accept the fact that circular references result in an error.

    Also, all these examples out of the books are not code problems, which is the main purpose of the site, to help with code issues and resolve them, there are plenty of sites on the internet that are used as reference sources, contain examples and so on.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Sep 2015
    Posts
    63
    Quote Originally Posted by \\.\ View Post
    We have have recently the same query crop up and despite repeately stating the facts, the OP still couldn't accept the fact that circular references result in an error.

    Also, all these examples out of the books are not code problems, which is the main purpose of the site, to help with code issues and resolve them, there are plenty of sites on the internet that are used as reference sources, contain examples and so on.
    Ok, I didn't know the main purpose of the site was to solve coding problems. Thank you, and sorry.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,426
    IMHO you are much better off by reading about the subject, looking at several examples and then have a go yourself.

    You will learn much faster by trying something out than asking many questions, if something doesn't work as expected then ask a question
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Sep 2015
    Posts
    63
    Quote Originally Posted by \\.\ View Post
    IMHO you are much better off by reading about the subject, looking at several examples and then have a go yourself.

    You will learn much faster by trying something out than asking many questions, if something doesn't work as expected then ask a question
    Ok, thank you on the suggestion. IMHO I think you are right

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center