Trusted answers to developer questions

Educative Team

**Memory management** is responsible for allocating segments of memory to programs at their request and then recovering it for reuse when it is not required anymore.

There are three main parts that make up a **vector**: its name, size, and capacity. The **name** is the identity of the vector. Whereas, the **size** and **capacity** are used to recognize the structure of the vector.

The size of the vector tells us how many elements are currently available in the vector.

```
<VectorName>.len();
```

The capacity of the vector tells us how many elements a vector can hold.

```
<VectorName>.capacity();
```

Note: The size of a vector is always less than or equal to the capacity of the vector.

When a vector is created, its size and capacity are zero. When we perform operations like the `push()`

function, the vector’s size increments by 1. With the `pop()`

and `remove()`

functions, the size of the vector decrements by 1.

Things are different when it comes to the capacity of the vector. When we use `push()`

the data, the vector checks if its size is equal to its capacity. If they are equal, then the vector updates its capacity by a factor of 2. However, the capacity of the vector remains the same when we are using the `pop()`

or the `remove()`

function.

A vector occupies space even after popping or removing all of its elements since it is able to retain its capacity. This may cause memory shortages for future operations. Let’s see this in the following example.

fn main() { // Creating a vector let mut vect = Vec::new(); // Pushing values 1-4 in vector for i in 0..5 { vect.push(i); println!("Pushing {}", i); } println!("Vector Capacity is {}", vect.capacity()); println!("\n\n"); // Removing values from vector for i in 0..5 { vect.remove(0); println!("Removing {}", i); } println!("Vector Capacity is {}", vect.capacity()); }

Memory inefficient way to handle a vector

`shrink_to_fit()`

methodRust provides access to a built-in method called `shrink_to_fit()`

. The ** shrink_to_fit() method** ensures that the capacity of the vector always equals to the size of the vector.

```
<VectorName>.shrink_to_fit();
```

The `shrink_to_fit()`

method does not return any value(s).

fn main() { // Creating a vector let mut vect = Vec::new(); // Pushing values 0-4 in vector for i in 0..5 { vect.push(i); println!("Pushing {}", i); } // Shrinking the capacity of vector vect.shrink_to_fit(); println!("Vector Capacity is {}", vect.capacity()); println!("\n\n"); // Removing values from vector for i in 0..5 { vect.remove(0); println!("Removing {}", i); } // Shrinking the capacity of vector vect.shrink_to_fit(); println!("Vector Capacity is {}", vect.capacity()); }

Memory efficient way to handle Vectors

It is best always to use the `shrink_to_fit()`

method at the end of the program, because it deallocates all the memory cells of the capacity of that vector.

RELATED TAGS

rust

vector

memory management

Copyright ©2022 Educative, Inc. All rights reserved

RELATED COURSES

View all Courses

Keep Exploring

Related Courses