Skip to content

Commit

Permalink
set MALLOC_ARENA_MAX to a lower dynamic value to prevent OOM
Browse files Browse the repository at this point in the history
The default behavior is 8x the number of detected CPUs . As Cloud
Foundry typically uses large host machines with smaller containers,
and the Java process is unaware of the difference in allocated CPUs,
the numbers are way off. This often leads to high native memory usage,
followed by a cgroup OOM killer event.

We go with Heroku's recommendation of lowering to a
setting of 2 for small instances and grow larger linearly with memory.

References:
- cloudfoundry/java-buildpack#163
- https://devcenter.heroku.com/articles/testing-cedar-14-memory-use
- cloudfoundry/java-buildpack#320
  • Loading branch information
Jouke Waleson committed Feb 21, 2018
1 parent 1622ac8 commit 67bde2e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def get_constants(metadata):
return constants


def set_jvm_memory(javaopts, vcap, java_version):
def set_jvm_memory(m2ee_section, vcap, java_version):
max_memory = os.environ.get('MEMORY_LIMIT')

if max_memory:
Expand Down Expand Up @@ -273,6 +273,7 @@ def set_jvm_memory(javaopts, vcap, java_version):
heap_size,
)
)
javaopts = m2ee_section['javaopts']

javaopts.append('-Xmx%s' % heap_size)
javaopts.append('-Xms%s' % heap_size)
Expand All @@ -284,6 +285,13 @@ def set_jvm_memory(javaopts, vcap, java_version):

logger.debug('Java heap size set to %s' % heap_size)

if os.getenv('MALLOC_ARENA_MAX'):
logger.info('Using provided environment setting for MALLOC_ARENA_MAX')
else:
m2ee_section['custom_environment']['MALLOC_ARENA_MAX'] = str(
max(1, limit / 1024) * 2
)


def _get_s3_specific_config(vcap_services, m2ee):
access_key = secret = bucket = encryption_keys = key_suffix = None
Expand Down Expand Up @@ -671,7 +679,7 @@ def set_up_m2ee_client(vcap_data):
m2ee.config.get_runtime_version()
)
set_jvm_memory(
m2ee.config._conf['m2ee']['javaopts'],
m2ee.config._conf['m2ee'],
vcap_data,
java_version,
)
Expand Down

0 comments on commit 67bde2e

Please sign in to comment.