我们正在开展一个大项目。在我们的开发过程中,我们面临着与Heap和PermGen Space相关的大问题。这里我们的错误消息:当前错误:java.lang.OutOfMemoryError:PermGen空间。
System.out.println("Initialazing..");
//Spring applicaton context
WebApplicationContext wac = (WebApplicationContext) AppContext.getApplicationContext();
// prepare path to internal ruby
String scriptsPath = wac.getServletContext().getRealPath(RUBY_PATH);
String jrubyHome = wac.getServletContext().getRealPath("WEB-INF" + File.separator + "jruby");
// Initializing Scripting container
ScriptingContainer container = new ScriptingContainer(isShared ? LocalContextScope.SINGLETHREAD
: LocalContextScope.THREADSAFE, LocalVariableBehavior.PERSISTENT);
// Configuring scriptingcontainer to avoid memory leaks
container.setCompileMode(RubyInstanceConfig.CompileMode.OFF);
System.setProperty("org.jruby.embed.compilemode", "OFF");
System.setProperty("jruby.compile.mode", "OFF");
// Setup ruby version
container.setCompatVersion(CompatVersion.RUBY1_9);
// Set jruby home
container.getProvider().getRubyInstanceConfig().setJRubyHome(jrubyHome);
List<String> loadPaths = new ArrayList<String>();
// load path
loadPaths.add(scriptsPath);
container.getProvider().setLoadPaths(loadPaths);
// ruby dispatcher initializing and run in simple mood
String fileName = scriptsPath + File.separator + "dispatcher_fake.rb";
// run scriplet
container.runScriptlet(PathType.ABSOLUTE, fileName);
// terminate container to cleanup memory without any effects
container.terminate();
container=null;
...
描述:上面的代码创建并配置Scripting容器。此方法在单独的线程中运行。我们使用4个ruby运行线程。如果我们使用相同的脚本容器并调用内部scriptlet(在java线程中调用internall方法),我们会遇到ruby变量的问题,因为它是可见的交叉线程。
JRuby的主要问题是:增加堆内存空间和perm gen内存空间。我们不能在ruby代码中调用任何系统的垃圾收集。
Bellow你可以找到我们的scriptlet的简单部分: 红宝石:
ENV['GEM_PATH'] = File.expand_path('../../jruby/1.9', __FILE__)
ENV['GEM_HOME'] = File.expand_path('../../jruby/1.9', __FILE__)
ENV['BUNDLE_BIN_PATH'] = File.expand_path('../../jruby/1.9/gems/bundler-1.0.18/bin/bundle', __FILE__)
require 'java'
require 'rubygems'
require "bundler/setup"
require 'yaml'
require 'mechanize'
require 'spreadsheet'
require 'json'
require 'rest-client'
require 'active_support/all'
require 'awesome_print'
require 'csv'
require 'builder'
require 'soap/wsdlDriver' rescue nil
ROOT_DIR = File.dirname(__FILE__)
require File.join(ROOT_DIR, "base", "xsi.rb")
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
module JavaListing
include_package "com.util.listing"
end
class A
include JavaListing
def run
1000.times do |index|
puts "iterating #{index}"
prop = JavaListing::Property.new
prop.proNo = 111
prop.remoteID = "1111"
prop.ownerID = "1111"
prop.advertiserID = "1111"
prop.title = "Atite"
prop.summary = "Asummury"
prop.description = "Adescription"
# prop.images << JavaListing::Image.new("111", "Acaption")
prop.lat = 12.23
prop.lng = 13.21
#prop.address = JavaListing::Address.new("Acity", "Acountry")
prop.location = "Alocation"
prop.policy = JavaListing::Policy.new("AcheckinAt", "AcheckoutAt")
prop.surfaceArea = "Asurfscearea"
prop.notes[index] = JavaListing::Note.new("Atitle", "Atext")
prop.order = "Aorder"
prop.map = JavaListing::Map.new(true, 14)
prop.units[index] = JavaListing::Unit.new("Aproptype", 2)
obj = Nokogiri::XML "<root><elements><element>Application Error #{index} </element></elements></root>"
end
end
end
A.new.run
与其他类型的scriptlet容器相同的perm gen:
使用JSR223创建属性
ScripHelperBase.java
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
Reader reader = null;
String fileName = scriptsPath + File.separator + "dispatcher_java.rb";
try {
reader = new FileReader(fileName);
} catch (FileNotFoundException ex) {
Logger.getLogger(ScriptHelperBase.class.getName()).log(Level.SEVERE, null, ex);
}
engine.eval(reader);
解决方法
列出项目
增加PermGen空间 PermGen空间增加到4Gb。很快它也变满了。
使用BSF创建属性
String fileName = scriptsPath + File.separator + "dispatcher_fake_ruby.rb";
String jrubyhome = "WEB-INF" + File.separator + "jruby";
BSFManager.registerScriptingEngine("jruby", "org.jruby.embed.bsf.JRubyEngine", new String[]{"rb"});
BSFManager manager = new BSFManager();
manager.setClassPath(jrubyhome);
try {
manager.exec("jruby", fileName, 0, 0, PathType.ABSOLUTE);
} catch (BSFException ex) {
Logger.getLogger(ScriptHelperBase.class.getName()).log(Level.SEVERE, null, ex);
}
结论:这意味着只是增加可用空间不能解决这个热点问题。
上述方法不允许将所需的存储器清除为原始状态。这意味着每个运行的其他脚本仍会增加填充的PermGen空间。
使用CompileMode = OFF
运行系统
container.setCompileMode(RubyInstanceConfig.CompileMode.OFF);
System.setProperty("org.jruby.embed.compilemode", "OFF");
System.setProperty("jruby.compile.mode", "OFF");