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

  • SEARCH
  • Home
  • 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 130343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:55:59+00:00 2026-05-11T05:55:59+00:00

I fear this is a really stupid question, but here goes: Why does the

  • 0

I fear this is a really stupid question, but here goes:

Why does the clear method in Java’s default LinkedList implementation bother to walk the list and unhook all the nodes? Why not just unhook the header and leave the rest of the list connected — the GC will get it anyway, no?

Here’s the method:

/**  * Removes all of the elements from this list.  */ public void clear() {     Entry<E> e = header.next;     while (e != header) {         Entry<E> next = e.next;         e.next = e.previous = null;         e.element = null;         e = next;     }     header.next = header.previous = header;     size = 0; modCount++; } 

Why walk it? Why not just skip to header.next = header.previous = header;?

Best I can figure is it helps the GC…? This link http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997442 sort of suggests that.

TIA…

  • 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. 2026-05-11T05:55:59+00:00Added an answer on May 11, 2026 at 5:55 am

    Their method ensures that even if other code still holds references to particular nodes, the other nodes will be GC’ed.

    Otherwise, even a single external reference to one of the nodes would prevent the entire chain from being collected.

    Also, other operations in the list might be going on simultaneously (e.g. views through subList() or Collections.unmodifiableList(), iterators), and this ensures that those things perceive the list as ’empty’ immediately.

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

Sidebar

Related Questions

I fear that this is a really stupid question but I am really stuck
I fear this is probably a bit of a dummy question, but it has
I fear this has been beaten to death, but I'm still struggling with the
I fear this might be a how long is a piece of string question,
Something really basic here I fear. Have three sliders used to control the colour
I've thought of a few ways of doing this but I want to get
This may sound very weird, but let's start with an example: <my:MagicWidget ui:field=someFieldName fieldName=someFieldName/>
Sorry if this has been covered - I've been looking for hours but I
I have seen a codebase recently that I fear is violating alignment constraints. I've
I always try to avoid to return string literals, because I fear they aren't

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.