Android单进程存在多个JDWP端口的原因及对应含义咨询
Great question—this is one of those subtle Android debugging quirks that catches even seasoned devs off guard. Let’s break down what’s going on here:
First, a quick clarification: When you run adb jdwp, you’re getting a list of process IDs associated with active JDWP debugging sessions, not just unique process IDs. So seeing the same PID pop up multiple times means that single process has multiple independent JDWP sessions running.
What Do These Extra JDWP Sessions Corresponding To?
Here are the most common scenarios:
Multiple Debuggers Connected Simultaneously
ART (Android’s runtime) supports parallel JDWP connections. If you already have Android Studio’s debugger attached to a process, then fire upjdbto connect to the same PID, ART will spin up a new JDWP session for the second debugger. Each connection works independently—so you can debug from both tools without interfering with each other.Embedded VMs or Plugin Execution Environments
Complex apps (like cross-platform games using Unity/Unreal, or plugin-based apps) sometimes spin up separate virtual machine instances inside the main process. These embedded VMs enable isolated execution for plugins, game logic, or third-party SDKs, and each one enables its own JDWP debugging endpoint. For example, a Unity app might have a main ART VM for Android platform code, plus a separate VM instance running the game’s managed C# code—each with its own JDWP session.System-Level Debugging Helpers
When you use system debugging flags (likeadb shell am set-debug-app -w your.package.nameto force the app to wait for a debugger), Android might create redundant JDWP listeners to ensure reliable debugger attachment. These are temporary sessions managed by the system, and they’ll disappear once the debugger connects or you disable the debug flag.
How to Identify Which Session Is Which?
If you want to map a JDWP session to its component:
- Forward a unique local port to each JDWP session (e.g.,
adb forward tcp:8000 jdwp:<PID>andadb forward tcp:8001 jdwp:<PID>for two sessions on the same PID). - Connect
jdbto each local port:jdb -attach localhost:8000 - Run the
threadscommand in jdb—look at the thread names and stack traces. For example, a Unity-related session will have threads with names likeUnityMain, while the main Android app session will have threads likemainorAndroidRuntime.
内容的提问来源于stack exchange,提问作者rainyday

