[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.
+
* Declaratively: Set the `max-size` attribute.
* Programmatically: 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.
+
* Declaratively: Set the `when-full` attribute.
* Programmatically: Use the `whenFull()` method.


.Declarative example

[source,xml,options="nowrap"]
----
include::config_examples/max_memory.xml[]
----

.Programmatic example

[source,java,options="nowrap"]
----
include::code_examples/MaxSizeMemory.java[]
----

.Reference

* 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:{dev_docs}#encoding_media_type[MediaType Configuration]
