java-native-access / java-native-access/jna

Optimize GC behaviour

Open
#694 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Java
Stars
8.9k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

JNA is making an insane amount of garbage when I'm trying to find a fake process by name.

Here is a screenshot of the allocations (about 100k.sec)

http://i.stack.imgur.com/u3D2I.png

Here is the test case (used 4.3.0 SNAPSHOT of JNA)

    import com.sun.jna.Native;
    import com.sun.jna.platform.win32.Kernel32;
    import com.sun.jna.platform.win32.Tlhelp32;
    import com.sun.jna.platform.win32.WinDef;
    import com.sun.jna.platform.win32.WinNT;

    /**
     * Created by Jonathan on 8/26/2016.
     */
    public class Main {

        public static void main(String[] args) {
            while (true)
                openProcess("doesntexist.exe");
        }

        private static final WinDef.DWORD DWORD_ZERO = new WinDef.DWORD(0);
        private static final Tlhelp32.PROCESSENTRY32 entry = new Tlhelp32.PROCESSENTRY32.ByReference();

        private static WinNT.HANDLE openProcess(String processName) {
            WinNT.HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL, DWORD_ZERO);
            try {
                while (Kernel32.INSTANCE.Process32Next(snapshot, entry)) {
                    String fileName = Native.toString(entry.szExeFile);
                    if (processName.equals(fileName))
                        return Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_ALL_ACCESS, true, entry.th32ProcessID.intValue());
                }
            } finally {
                Kernel32.INSTANCE.CloseHandle(snapshot);
            }
            return null;
        }

    }

And finally here is the memory snapshot
https://dl.dropboxusercontent.com/u/91292881/ShareX/2016/08/JNA%204.3.0.snapshot

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Main reproduction and its Kernel32.INSTANCE CreateToolhelp32Snapshot, Process32Next, Native.toString, and CloseHandle calls. Run the loop while profiling allocations to identify the source of the garbage during a failed process lookup. Done means the same lookup behavior with materially lower allocation and GC activity.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
operating-systems, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.