简体   繁体   中英

Can JVM memory settings be set on an AWS Lambda?

We are getting

java.lang.OutOfMemoryError: Metaspace

Since we are not using our full memory space, we figure it is the metaspace size. I have seen a few posts that say we can set "some" jvm settings but I want to set some of these:

java -XX:MaxMetaspaceSize=192mb (just example sizes)
  1. Is it possible to set these settings?
  2. Where in the yml can I do that?

Update

Here are the current settings from the lambda (via: ManagementFactory.getRuntimeMXBean().getInputArguments())

-XX:MaxHeapSize=445645k, -XX:MaxMetaspaceSize=52429k, -XX:ReservedCodeCacheSize=26214k, -XX:+UseSerialGC, -Xshare:on, -XX:SharedArchiveFile=/var/lang/lib/server/runtime.jsa, -XX:-TieredCompilation, -Djava.net.preferIPv4Stack=true

You're approaching this from the wrong direction. Per the Java8 docs :

The amount of native memory that can be used for class metadata is by default unlimited. Use the option MaxMetaspaceSize to put an upper limit on the amount of native memory used for class metadata

So, the only thing that explicitly configuring metaspace size does is limit the amount of metaspace memory; it won't increase it. If you want to give more memory to metaspace, you need to decrease predefined size of the rest of the heap.

However, that's the wrong thing to do.

The real problem is that you're doing something that loads more classes than can fit in available metaspace. The most likely cause is that you've just picked a memory configuration that's too small for your deployment bundle. You didn't say what your configured value was, but if I had to guess it's around 512 MB. Try 1024 or 1536.

It's also possible that you -- or one of the libraries that you use -- is dynamically creating classes in an unconstrained manner. In this case it doesn't matter how much memory you give to your Lambda, you'll eventually run out of space.

The way to diagnose this is to run your code outside of Lambda, using the TraceClassLoading and TraceClassUnloading flags. You're looking for classes that have "generated" names, typically random sequences of digits or letters.

There is a way to setup max memory usage. Note: there are limits, min is 128 and max 3008 MB. See details here

Using serverless.yml

provieder:aws
  memorySize: 512 # Overwrite the default memory size. Default is 1024

Using SAM template.yml

LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      MemorySize: 512

If you plan to have a very custom setup there is custom runtime - https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html You need a bootstrap file which is the entry point and where you could define how the application is run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM