2 Replies - 189 Views - Last Post: 07 June 2020 - 04:42 AM

#1 Soumikbhat   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 83
  • Joined: 01-September 13

Reading MaxMind DB throws Java OutOfMemory Error

Posted 06 June 2020 - 09:36 AM

I am running a Java Spring application on a micro ec2 instance on AWS. The use case is to detect the city and country for the client's i/p address. I am reading the MaxMind Db for this. However, reading the database from code throws an OutOfMemory error for Java Heap Space.

This is what I have tried so far:

Created a singleton class to serve the database reader object

package com.people.chat.Model;

import java.io.IOException;
import java.io.InputStream;

import com.maxmind.geoip2.DatabaseReader;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class MaxMindDBReader {

    private static DatabaseReader dbReader = null;

    private MaxMindDBReader() {

    }

    public static DatabaseReader getReader() {
        if (dbReader == null) {
            try {
                Resource resource = new ClassPathResource("GeoLite2-City.mmdb");
                InputStream inputStream = resource.getInputStream();
                dbReader = new DatabaseReader.Builder(inputStream).build();
                resource = null;
                System.gc();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return dbReader;
    }

}


Now in my serviceImpl file, I am getting the city and country

public static String getCity(String ip) throws IOException, GeoIp2Exception {

        DatabaseReader dbReader = MaxMindDBReader.getReader();

        InetAddress ipAddress = InetAddress.getByName(ip);
        CityResponse response = dbReader.city(ipAddress);

        String countryName = response.getCountry().getName();
        String cityName = response.getCity().getName();
        String postal = response.getPostal().getCode();
        String state = response.getLeastSpecificSubdivision().getName();

        return cityName + ", " + countryName;
        
    }


Any improvements/alternate services that can be used for this?

This post has been edited by Soumikbhat: 06 June 2020 - 09:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading MaxMind DB throws Java OutOfMemory Error

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7389
  • View blog
  • Posts: 24,940
  • Joined: 05-May 12

Re: Reading MaxMind DB throws Java OutOfMemory Error

Posted 06 June 2020 - 06:34 PM

Seems very Java specific. Moving to the Java forums...
Was This Post Helpful? 0
  • +
  • -

#3 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Reading MaxMind DB throws Java OutOfMemory Error

Posted 07 June 2020 - 04:42 AM

Can you copy the error message stack trace and paste it here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1