[id='configure_eviction_size-{context}']
= Configuring the Maximum Amount of Memory for {brandname} Caches
Limit the size of the data container for cache entries to a maximum amount of
memory.

.Procedure

. Configure your {brandname} cache to use a storage format that supports binary encoding.
+
You must use a binary storage format to perform eviction based on the maximum
amount of memory.
+
. Configure the maximum amount of memory, in bytes, that caches can use before
{brandname} performs eviction.
+
* Declarative: Set the `max-size` attribute.
* Programmatic: Use the `maxSize()` method.
+
. Optionally specify a byte unit of measurement. The default is B (bytes). Refer to the configuration schema for supported units.
. Configure an eviction strategy to control how {brandname} removes entries.
+
* Declarative: Set the `when-full` attribute.
* Programmatic: Use the `whenFull()` method.


.Declarative configuration
[source,xml,options="nowrap",subs=attributes+,role="primary"]
----
include::config_examples/max_memory.xml[]
----

.Programmatic configuration
[source,java,options="nowrap",subs=attributes+,role="secondary"]
----
include::code_examples/MaxSizeMemory.java[]
----

[role="_additional-resources"]
.Additional resources
* link:{configdocroot}[{brandname} Configuration Schema Reference]
* link:{javadocroot}/org/infinispan/configuration/cache/EncodingConfiguration.html[org.infinispan.configuration.cache.EncodingConfiguration]
* link:{javadocroot}/org/infinispan/configuration/cache/MemoryConfigurationBuilder.html[org.infinispan.configuration.cache.MemoryConfigurationBuilder]
* link:{encoding_docs}[Cache Encoding and Marshalling]
