Home

shrinkto

Shrinkto is a programming concept used to reduce the allocated memory capacity of a dynamic container to match its current size. The term is most often encountered in the form shrink_to_fit in language standard libraries, though the exact name and behavior vary by language.

In C++, containers like std::vector and std::basic_string expose shrink_to_fit as a non-binding request to reduce capacity

In Rust, Vec<T> provides shrink_to_fit to reduce capacity to match length, which may trigger a reallocation. Other

Practical guidance: shrinkto can help reduce memory usage after the container has grown and then shed many

Limitations and alternatives: Not all containers support shrink_to_fit, and behavior is implementation-dependent. A common alternative to

Related concepts include capacity, size, and general memory management, as well as language-specific implementations such as

to
fit
size.
The
standard
notes
that
the
request
may
have
no
effect;
an
implementation
may
decide
to
skip
reallocation.
When
it
does
take
effect,
it
typically
reallocates
memory
and
moves
the
existing
elements
to
a
new
buffer,
potentially
freeing
unused
capacity.
languages
have
similar
mechanisms
under
different
names,
such
as
shrink
or
shrinkToFit.
elements,
but
it
can
be
expensive
due
to
allocations
and
element
movement.
Because
the
operation
is
not
guaranteed
to
free
memory
immediately,
it
should
be
used
when
memory
pressure
exists
or
a
predictable
footprint
is
required.
guarantee
a
smaller
capacity
is
to
copy
the
elements
into
a
freshly
constructed
container
and
swap
it
with
the
old
one.
shrink_to_fit
in
C++
and
Vec::shrink_to_fit
in
Rust.