首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maxmind DatabaseReader Java堆空间

Maxmind DatabaseReader Java堆空间
EN

Stack Overflow用户
提问于 2020-01-02 07:43:47
回答 1查看 625关注 0票数 1

我尝试在spring boot中使用GeoLite2-City.mmdb来获取位置数据。为此,我在配置文件中创建了一个类似以下内容的bean:

代码语言:javascript
复制
        @Bean   
        public static synchronized DatabaseReader mmdbReader() throws IOException { 

               if ( reader != null ) {
                   return reader;
               }

                ResourceLoader resourceLoader = new DefaultResourceLoader();

                Resource resource = resourceLoader.getResource("classpath:static/mmdb/GeoLite2-City.mmdb");
                InputStream database = resource.getInputStream();

               reader = new DatabaseReader.Builder(database).fileMode(com.maxmind.db.Reader.FileMode.MEMORY)
                       .withCache(new CHMCache()).build(); 

           return reader;
        }

我还创建了一个util函数来检索位置,如下所示:

代码语言:javascript
复制
     public static GeoLocation getLocation(HttpServletRequest request, DatabaseReader databaseReader) throws IOException, GeoIp2Exception{

      if(request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") || request.getRemoteAddr().equals("127.0.0.1")) {
          return new GeoLocation("localhost", "Redmond", "USA", 47.673988, -122.121513);
      }

      InetAddress ipAddress = InetAddress.getByName(request.getRemoteAddr());

      CityResponse response = databaseReader.city(ipAddress);

      Country country = response.getCountry();
      City city = response.getCity();
      Location location = response.getLocation();

      return new GeoLocation(ipAddress.toString(), city.getName(), country.getName(), location.getLatitude(), location.getLongitude());
      }

但是,每次我部署应用程序时,我都会收到这个错误

代码语言:javascript
复制
 Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3236)
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
    at com.maxmind.db.BufferHolder.<init>(BufferHolder.java:64)
    at com.maxmind.db.Reader.<init>(Reader.java:89)
    at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:64)
    at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:54)

    at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:160)
    at com.microsoft.apie.configuration.ApplicationConfiguration.mmdbReader(ApplicationConfiguration.java:104)

我的java设置是:

代码语言:javascript
复制
  -Xms512m -Xmx2048m

你能帮个忙吗?

EN

回答 1

Stack Overflow用户

发布于 2020-01-03 07:48:47

我通过这样做解决了这个问题:

a.我从jar中删除了GeoLite2-City.mmdb文件,并将其放在与jar相同的文件夹中

b.我重构了方法,并这样做:

代码语言:javascript
复制
    @Bean  
    public static synchronized DatabaseReader mmdbReader() throws IOException { 

              if ( reader != null ) {
                  return reader;
              }

               /*ResourceLoader resourceLoader = new DefaultResourceLoader();

               Resource resource = resourceLoader.getResource("classpath:static/mmdb/GeoLite2-Country.mmdb");

               InputStream database = resource.getInputStream();
              */
               FileSystemResource resource = new FileSystemResource("/path/to/file/GeoLite2-City.mmdb");

              reader = new DatabaseReader.Builder(resource.getFile()).fileMode(com.maxmind.db.Reader.FileMode.MEMORY_MAPPED)
                      .withCache(new CHMCache()).build(); 

          return reader;
   }

c.重新打包应用程序,它可以正常工作!

感谢Strelok的建议

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59557001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档