JRuby PermGen空间不足,甚至在Ruby代码中使用java对象 [英] JRuby PermGen out of space even use java objects in ruby code

查看:69
本文介绍了JRuby PermGen空间不足,甚至在Ruby代码中使用java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:

如果我们使用Java对象jruby,也要获得permgen:

If we use java objects jruby get permgen too:

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;

脚本:

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'

ROOT_DIR = File.dirname(__FILE__)
require File.join(ROOT_DIR, "base", "xsi.rb")

# require java classes
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}"
      # java class property
      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)
      # 
      doc = Jsoup.parse("<root><elements><element>Application Error  #{index}  </element></elements></root>")
  end
end

您可以看到我们正在使用所有Java对象,但是Perm仍然发生内存泄漏

As you can see we are using all java objects but Perm get aout of memory leaks still happens

如果我们在spring应用程序中创建对象,然后将Application Context传递给ruby以便将来获取bean,这将无法解决此问题

If we create objects in spring application and pass Application Context to ruby for getting beans in future this didn`t ressolve this issues

在脚本编写容器中,设置ApplicationContext context = Spring应用程序上下文

In scripting container set ApplicationContext context = Spring application context

container.put("$context", context);

红宝石代码

ruby code

bean = $context.getBean("some_name")
bean.myMethod()

推荐答案

解决方案是使用从当前JVM启动的单独JVM:

Solution is to use separate JVM started from current JVM:

启动新的JVM

这篇关于JRuby PermGen空间不足,甚至在Ruby代码中使用java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆