Skip to content

Instantly share code, notes, and snippets.

@sgykfjsm
Last active August 29, 2015 14:16
Show Gist options
  • Save sgykfjsm/bd2753a2eef8e84fc983 to your computer and use it in GitHub Desktop.
Save sgykfjsm/bd2753a2eef8e84fc983 to your computer and use it in GitHub Desktop.
JVMのパラメータチューニングのために"java.lang.OutOfMemoryError"を発生させたい
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
public class OOM1 {
private static void output() {
java.util.List<MemoryPoolMXBean> beans =
ManagementFactory.getMemoryPoolMXBeans();
for(MemoryPoolMXBean bean : beans) {
MemoryPoolMXBean permgenBean = null;
if(bean.getName().toLowerCase().indexOf("perm gen") >= 0) {
permgenBean = bean;
MemoryUsage currentUsage = permgenBean.getUsage();
int percentageUsed = (int)((currentUsage.getUsed() * 100)
/ currentUsage.getMax());
System.out.print("FreeMem: " + Runtime.getRuntime().freeMemory());
System.out.println(" Permgen " + currentUsage.getUsed() +
" of " + currentUsage.getMax() +
" (" + percentageUsed + "%)");
break;
}
}
}
public static void main(String[] args) {
output();
main(new String[] { // http://stackoverflow.com/a/12882135
(args[0] + args[0]).intern()
});
}
}
@sgykfjsm
Copy link
Author

sgykfjsm commented Mar 7, 2015

javac ./OOM1.java && \
java -XX:+TraceClassLoading \
        -XX:+TraceClassUnloading \
        -Xmx456M \
        -Xms456M \
        -XX:PermSize=4M \
        -XX:MaxPermSize=4M \
        OOM1 aaa

だと

#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 100663312 bytes for Chunk::new
# An error report file with more information is saved as:
# /tmp/jvm-5224/hs_error.log

が発生して、

javac ./OOM1.java && \
java -XX:+TraceClassLoading \
        -XX:+TraceClassUnloading \
        OOM1 aaa

だと

java.lang.OutOfMemoryError: Java heap space

になる。

javac ./OOM1.java && \
java -XX:+TraceClassLoading \
        -XX:+TraceClassUnloading \
        -XX:PermSize=2M \
        -XX:MaxPermSize=2M \
        OOM1 aaa

だと

Error occurred during initialization of VM
java.lang.OutOfMemoryError: PermGen space

が発生して、そもそも処理が起動しない。
PermGen spaceが発生する場合はJVMは最初から起動できない?JVMが起動して処理が行われている途中にPermGen spaceが発生するのはどんな時?

@sgykfjsm
Copy link
Author

sgykfjsm commented Mar 7, 2015

テストした環境は以下のとおり

[vagrant@nativememory share]$ uname -a
Linux nativememory 2.6.32-504.8.1.el6.x86_64 #1 SMP Wed Jan 28 21:11:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[vagrant@nativememory share]$ cat /etc/redhat-release
CentOS release 6.6 (Final)
[vagrant@nativememory share]$ java -version
java version "1.7.0_75"
OpenJDK Runtime Environment (rhel-2.5.4.0.el6_6-x86_64 u75-b13)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)

@sgykfjsm
Copy link
Author

sgykfjsm commented Mar 7, 2015

ついでにVagrantfileもアップしておく。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
$script = <<SCRIPT

echo Installing depedencies...
sudo yum check-update
sudo yum -y update
sudo yum install -y java-1.7.0-openjdk.x86_64 java-1.7.0-openjdk-devel.x86_64

SCRIPT
#-------
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos65"
  # curl --location --remote-name http://dl.dropbox.com/u/9227672/CentOS-6.0-x86_64-netboot-4.1.6.box
  config.vm.box_url = "/Users/sgyk/local/vagrant/box/CentOS-6.0-x86_64-netboot-4.1.6-Minimal.box"
  config.cache.auto_detect = true
  config.vm.provision "shell", inline: $script
  config.vm.synced_folder "share/", "/home/vagrant/share"

  config.vm.define :serf1 do |serf1|
    serf1.vm.hostname = "nativememory"
    serf1.vm.network :private_network, ip: "192.168.56.201"
    serf1.vm.provider "virtualbox" do |v|
      v.customize ["modifyvm", :id, "--memory", 512]
      v.name = "nativememory"
    end
  end

end

@sgykfjsm
Copy link
Author

sgykfjsm commented Mar 7, 2015

余談だけど、Vagrantで共有フォルダが作れない場合は、ゲストOSの/etc/yum.confで

exclude=kernel*

# exclude=kernel*

にして、

sudo yum update -y kernel

をしてから

$ vagrant plugin install vagrant-vbguest
$ vagrant reload --provision

とすれば良い。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment