Ubuntu – How to free the (unused) memory

amazon ec2amazon-web-servicesmemorysshUbuntu

Being a newbie in working with amazon ec2, I am trying to make my application work. I've connecting a few times by ssh, I've been running and stopping the server, sometimes the connection was lost, and some other things…

At some point I got the error from JVM (my application uses Scala) that there was not enough free memory and it couldn't allocate it to run itself. What I did was rebooting the server. It worked. But I figure there should be more effective way(s). Are there any?

UPDATE:

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5550000, 715849728, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 715849728 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /home/ubuntu/my-app/hs_err_pid8668.log

Best Answer

I'd look at two changes:

  • I'm pretty sure you need to tune the memory usage of your JVM (with, to start, -Xmx) to match the machine you're on. Make sure you're not asking for way more memory than your VM has.
  • Frob your overcommit settings. A talk from Re:invent 2013 suggests overcommit_memory=2 and overcommit_ratio=80. Getting into a bad place with overcommit will cause malloc to fail like you're seeing.