4

I'm trying to find a native memory leak in my production application. The problem is to get know which method in my code does unsafe.allocateMemory(size) without unsafe.freeMemory(startIndex)

This is for Ubuntu 18.04, java version "1.8.0_191".

// Example of "Unlimited array" 
class DirectIntArray implements Closeable {

    private final static long INT_SIZE_IN_BYTES = 4;

    private final long startIndex;

    private final Unsafe unsafe;

    public DirectIntArray(long size) throws NoSuchFieldException, IllegalAccessException {
        unsafe = getUnsafe();
        startIndex = unsafe.allocateMemory(size * INT_SIZE_IN_BYTES);
        unsafe.setMemory(startIndex, size * INT_SIZE_IN_BYTES, (byte) 0);
    }

    @Override
    public void close() throws IOException {
        unsafe.freeMemory(startIndex);
    }

    public void setValue(long index, int value) {
        unsafe.putInt(index(index), value);
    }

    public int getValue(long index) {
        return unsafe.getInt(index(index));
    }

    private long index(long offset) {
        return startIndex + offset * INT_SIZE_IN_BYTES;
    }

    private static Unsafe getUnsafe() throws IllegalAccessException, NoSuchFieldException {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        return (Unsafe) theUnsafe.get(null);
    }
} 



public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {

        int cnt = 0;
        System.out.println("Use big array in off-heap without freeing memory");
        while (true) {
            useWithLeak(MB);
            Thread.sleep(10);
            System.out.println(++cnt + "MB Allocated");
        }
    }

    public static void useWithLeak(long len) {
        try {
            DirectIntArray arr = new DirectIntArray(len);
            arr.setValue(1, 111);
            arr.setValue(len, 222);

            System.out.println("Read from off-heap values: " +  arr.getValue(1) + ", " + arr.getValue(len));
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

So I wrote the code that does native memory leak, and I want to find a method that does it. I use jemalloc and I have a .gif file that I've got according to this instruction. I've got an image

enter image description here

So how could I guess, that 0x00007f2e42297ea7 is useWithLeak method? Is it real?

htshame
  • 6,599
  • 5
  • 36
  • 56
Alexandr
  • 41
  • 2

0 Answers0