Getting out of memory exception while creating multiple couchbase Environments

I am getting “java.lang.OutOfMemoryError: Java heap space” error in my project Along with multiple warnings like this
“com.couchbase.client.core.env.DefaultCoreEnvironment. More than 1 Couchbase Environments found (48), this can have severe impact on performance and stability. Reuse environments!”

Is it because of the creation of multiple Couchbase Environments that the heap space is running out ??

I’m not sure about the memory error, but let’s address a deeper problem first: it looks like you have created 48 Cluster instances!?

If you really need to talk with 48 different clusters (not nodes, so somehow I doubt it), then you should if possible pass a common CouchbaseEnvironment at construction.
The environment dictates configuration like global timeouts, I/O tuning, etc… (see the doc). You just have to limit the number of environments to the minimum (preferably 1) and share them by passing them to the cluster constructors.

If you don’t really have more than 1 cluster, then it’s probably that you recreate them when it’s not needed. IIRC you use Spring so the Cluster should be a bean with singleton scope… Not in session scope or anything like that.

In Spring, to share the environment, you’d have to create a singleton bean for the environment and reference it as a constructor parameter of your cluster(s) bean(s).
Note that it will be more difficult with xml configuration since the environment is created by chain-calling builder methods… With annotation config it should look like this (random example values):

public CouchbaseEnvironment envProd() {
    return DefaultCouchbaseEnvironment.builder()