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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:35:15+00:00 2026-05-30T07:35:15+00:00

Actually I have millions of vector objects in my program. In default for each

  • 0

Actually I have millions of vector objects in my program. In default for each vector, the system will assign more space than it actually needed since those vectors are read-only after they are finish loading.

So I want to shrink their capacity to save memory. a typical way is using vector.swap() method as described in this question:

 std::vector<T> tmp(v);    // copy elements into a temporary vector
           v.swap(tmp);              // swap internal vector data

I have tried this code, but found the .swap() operation doesn’t reduce the memory cost actually. (I looked at the Private Working Set size in the task manager to get the process’s memory usage)

Why is that?
UPDATE”
I am using VS 2008, and I have added some lines to output the current capacity during runtime. As seen in below code and its output, the vec.capacity did shrink to 10 after swap, but this doesn’t reflect in task manager.

Or maybe just like @Adam Rosenfield said, this change didn’t return the freed space to OS? Or I shouldn’t use the Task Manager to look up its memory usage? I hope this swap() operation can have the effect as same as delete pointer_to_a_big_vec which will directly free the memory.

My test sample is here:

void vecMemoryTest()
{
  vector<int> vec;

  //make it big!
  for(int i = 0; i <= 100000; i++){vec.push_back(i);}
  for(int i = 0; i <= 100000; i++){vec.push_back(i);}

  cout << "Before .resize() : " << vec.capacity() << endl;

  //OK now I only need the first 10 elements
  vec.resize(10);   

  cout << "After .resize() : " << vec.capacity() << endl;

  //So try to free other spaces
  vector<int> tmp(vec.begin(), vec.end());



  vec.swap(tmp);    // now should free wasted capacity
            // But in fact it doesn't

  cout << "After .swap() : " << vec.capacity() << endl;
}

and the output is:

Before .resize() : 207382
After .resize() : 207382
After .swap() : 10

  • 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-05-30T07:35:17+00:00Added an answer on May 30, 2026 at 7:35 am

    Check the capacity() of the vector. You will most probably find that the vector in fact reduced its memory usage. What you are seeing is probably malloc() implementation not releasing memory back to the OS.

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

Sidebar

Related Questions

Actually i have millions of records of people in my server database and i
I have a system which will return all users from the database and order
I actually have an answer to my question but it is not parallelized so
I actually have two questions regarding the same problem but I think it is
I actually have right now two questions: 1) What font faces are preferred for
I actually have two questions regarding exception/error handling in the iPhone app that I
I actually have 2 queries: How do I display the data of a variable
I have heard that it's best not to actually have any html in your
I have a JTree with multiple roots (Of course, I actually have an invisible
This is a minimal test case of some code that I actually have. It

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.