1

Suppose getLargeArray() returns a very large array of many items. I declared a variable called "items" and initialize it with a very large array. Then, I reassign a small array to the variable, items, like the following:

var items = getLargeArray();

items = ["dog","cat","pig"];

What will happen to the memory that stores the large array? Does items points to the memory that holds the data? Or, is it a pointer to the address that holds the first element of the array? If I keep doing that, what will happen to my application on Chrome?

Rongeegee
  • 866
  • 3
  • 10
  • 30
  • 1
    **big** topic. Where to begin... maybe read the v8 blog about the garbage collector [here](https://v8.dev/blog/trash-talk)? – Wyck Jun 19 '21 at 02:54
  • 1
    Short answer: if an object (such as your array) is no longer reachable, it is eligible for garbage collection. – Wyck Jun 19 '21 at 03:00
  • `items` is a _reference_ to the array, not a pointer to the first element. – Sebastian Simon Jun 19 '21 at 03:07
  • 1
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+garbage+collection) of [What is JavaScript garbage collection?](/q/864516/4642212) and [When and How JavaScript garbage collector works](/q/58100536/4642212). Have you read these? Did these not answer all your questions? – Sebastian Simon Jun 19 '21 at 03:07
  • @SebastianSimon These posts don't explain how garbage collection work on modern browsers specifically. – Rongeegee Jun 19 '21 at 18:24

0 Answers0