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 8299149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:02:44+00:00 2026-06-08T16:02:44+00:00

I have a mutable class which has a private List<T> field inside. In the

  • 0

I have a mutable class which has a private List<T> field inside. In the Reset() method of my class, should I clear the list using its Clear() method or just assign its field a new list? Note that the list is not public and is only used by the class itself. So assigning a new list should make the old one unreachable. Since the Clear() method is an O(n) operation, I wonder what is the downside of just assigning a new list over it.

  • 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. Editorial Team
    Editorial Team
    2026-06-08T16:02:48+00:00Added an answer on June 8, 2026 at 4:02 pm

    The only downside I can think of is if you need to use the list again you’ll have to allocate new space for it.

    Nulling it will just make the list and its content (assuming no other references) eligible for GC. Clearing it would remove the items but leave the memory allocated.

    Personally I tend to null the thing as even if I need it again, the size will have changed completely.

    Update: Pertaining to the comments below, you state these objects will be managed in a pool of objects. I would advise creating a small profiling console app to get the final answer. The discussion is now stepping into the specifics of your implementation and the intended usage of the object pool, which could easily change the answer.

    Generally speaking, if you have lists that don’t change in length much and are always going to be required, I would use Clear to avoid allocating new memory for the lists. If the list length is liable to a lot of change, or usage is sometimes sparse – I would favour nulling it in order to reclaim memory and gain some minor benefits via lazy instantiation of the lists.

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

Sidebar

Related Questions

I have the following code which has a mutable Person class, String and a
I have a mutable class that I'm using as a key to a generic
Context: I have a TableLayout (created using XML), which has one TableRow, which has
I have a sorted mutable array of a class called Topic. The topics represent
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I have a mutable array that has about 100 objects in it and each
What I have? I have a simple web part which has a Table .
I have an array which has 200 objects in it on page load i
I have this method to save a mutable array named myWallet that contains instances
I have the following set-up: A Window that has a splitView in which I

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.