1

I have a VPS (Linux Server) where I downloaded a 64-bit version of Java. On my terminal, I am able to run commands such as:

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-0.b17.el6_7.x86_64/jre/bin/java -version

And receive the following output:

openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed 

So to test this for my website, I used the following php:

<?php 
  $output = array();
  exec('/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-0.b17.el6_7.x86_64/jre/bin/java -version', $output); 
  foreach($output as $line) {
    echo $line;
    echo '<br/>';
  } 
?>

But receive the following error:

Error occurred during initialization of VM
Could not allocate metaspace: 1073741824 bytes

So far I have tried the following:

1) I've updated permissions on java so that:

stat -c "%a %n" /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-0.b17.el6_7.x86_64/jre/bin/java

Returns:

755 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-0.b17.el6_7.x86_64/jre/bin/java

2) I've turned php safe-mode off

3) I've changed the max memory in my php.ini file to 1.5GB

What causes this error and how do I get the java program to run without error?

10
  • 1
    when things work on the command line but not exec(), its often a user privileges issue. Commented Jan 5, 2016 at 20:12
  • or enironment variables. did you run "whoami" from PHP and command line to see if the correct user is running it? Commented Jan 5, 2016 at 20:16
  • @Dagon I'd like it to be available for all users to run, which is why I changed the priveleges to 755. Was that correct? I'm new to all this so I may be missing something. ls -l java Returns: -rwxr-xr-x 1 root root 5128 Oct 21 16:39 Commented Jan 5, 2016 at 20:21
  • 1
    @pgmann says that total is 1.5G, used is 1.5G and free is 40M. Would that suggest a memory leak? Commented Jan 5, 2016 at 20:48
  • 1
    I spoke with my web host and they agreed with what you just mentioned about the -/+ buffers and that only 0.2% of my RAM is being used. In my -/+ buffers/cache, I have 126M used and 1.4G free Commented Jan 5, 2016 at 21:07

2 Answers 2

1

I was able to reproduce this error using bluehost's webspace, and fix it.

I was running a heavy java program though the PHP method: shell_exec and java produced an error:

Error occurred during initialization of VM
Could not allocate metaspace: 1073741824 byte

Linux version:

uname -a
Linux box1.bluehost.com 1.2.50-39.ELK6.x86_64 #1 SMP Mon Nov 2 03:10:26 CST 2015 x86_64 x86_64 x86_64 GNU/Linux

PHP version:

php --version
PHP 5.4.43 (cgi-fcgi) (built: Jul 13 2015 15:00:01)
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies

Java version:

java -version
java version "1.8.0_71"

Reproduce the problem:

Make a test.php script under public_html, like this:

<?php
    shell_exec('java HungryJavaProgram > /home/user/mylog.log');
?>

The java program called HungryJavaProgram allocates a gigabyte of memory.

From the browser, visit yoursite.com/test.php to run that php file. The java program reports:

Error occurred during initialization of VM
Could not allocate metaspace: 1073741824 byte

Why is this happening?

The java program has requested more memory than PHP has authorized.

Solution #1, increase the memory available:

Find out how much memory PHP has authorized programs running inside shell_exec can consume by making this PHP script and visiting it in the browser:

<?php
   phpinfo();
?>

Look at: memory_limit to see the default is: 128M

Find your php.ini under public_html and find the memory_limit variable and set that to 2048M. On bluehost the changes take effect immediately. Understand now that one rogue program might now bring down your server. So keep that as low as possible.

Workaround solution #2, use PHP fastcgi.

In the public_html directory look in the .htaccess file, I saw this:

AddHandler application/x-httpd-php54 .php

Comment that out and replace it with this:

AddHandler fcgid54-script .php

And now the java program runs without error. Even though the memory limit remains at 128M, PHP doesn't seem to enforce that limit on java programs running from within shell_exec("java myprogram").

Sign up to request clarification or add additional context in comments.

1 Comment

Now I'm facing the same issue. i already increased PHP memory limit to 2GB, even though this error coming. I'm using PHP 7.3, openjdk version is "12" with x64 server, OS is AlmaLinux 8. I know this answer is very old but any idea? openjdk version "12" 2019-03-19 OpenJDK Runtime Environment (build 12+33) OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
1

Your answer is in the error message:

 Could not allocate metaspace: 1073741824 bytes

You are exceeding the amount of memory you are allowing Apache and PHP to use. There are limits setup in php.ini and the Apache user might have a ulimit set.

Stock PHP install default is typically set to a limit of 2MB. Java is looking for 1G of memory. Change the memory limit in PHP to at least 1G.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.