Skip to content

Using FileMode.MEMORY_MAPPED leaves the file locked, even after closing the reader #11

@ikalatch

Description

@ikalatch

When using FileMode.MEMORY_MAPPED, the .mmdb file on the file system is not available for manipulation (renaming, moving, deleting) even after the reader is closed.

Using FileMode.MEMORY doesn't suffer from this problem. See sample unit test below. Could be OS-related (using JDK7 on Win7, MaxMindDB: v.0.3.0, GeoIP2: v.0.6.0).
Ran into the issue while working on automatically updating the .mmdb file once a month in a long-running process.

import java.io.File;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import com.maxmind.db.Reader.FileMode;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.DatabaseReader.Builder;

public class _GeolocationFileUpdate
{
    private static final String GEO_LITE2_CITY_MMDB = "GeoLite2-City.mmdb";
    private static final String GEO_LITE2_CITY2_MMDB = "GeoLite2-City2.mmdb";
    private static final String DIR = "c://Temp/"; //adjust if on a diff. OS. Place the .mmdb file here
    private static final File f1 = new File( DIR + GEO_LITE2_CITY_MMDB );
    private static final File f2 = new File( DIR + GEO_LITE2_CITY2_MMDB );

    @Test
    public void testSwitchingMem()
    {
        try
        {
            // make sure the file name is reverted after renaming:
            f2.renameTo( f1 );

            Builder builder = new DatabaseReader.Builder( f1 );
            builder.fileMode( FileMode.MEMORY );
            DatabaseReader reader = builder.build();
            reader.close();

            // make sure file is not in use by a process:
            Assert.assertTrue( f1.renameTo( f2 ) );
        }
        catch ( IOException e )  {  e.printStackTrace();  }
    }

    @Test
    public void testSwitchingMemMapped()
    {
        try
        {
            // make sure the file name is reverted after renaming:
            f2.renameTo( f1 );

            Builder builder = new DatabaseReader.Builder( f1 );
            builder.fileMode( FileMode.MEMORY_MAPPED );
            DatabaseReader reader = builder.build();
            reader.close();

            // make sure file is not in use by a process:
            // ! test fails on the line bellow, file is in use !:
            Assert.assertTrue( f1.renameTo( f2 ) );
        }
        catch ( IOException e ) { e.printStackTrace(); }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions