简体   繁体   English

Java 线程是否有自己的进程 ID?

[英]Does a Java Thread have its own process ID?

I want to get the process ID of a Thread to see how much memory it takes.我想获取一个线程的进程 ID,看看它需要多少 memory。

It depends a lot on the OS and how it manages threads.这在很大程度上取决于操作系统以及它如何管理线程。 Theoretically it also depends on how the JVM implements threads, but all modern JVMs implement them as native threads.从理论上讲,它还取决于 JVM 如何实现线程,但所有现代 JVM 都将它们实现为原生线程。

On Linux each thread will used to get its own process ID, but most tools hide all but one thread per process (ie you don't usually see them unless you explicitly ask for them, ps uses the -m flag for example).在 Linux 上,每个线程 都将 用于获取自己的进程 ID,但大多数工具会隐藏每个进程除了一个线程之外的所有线程(即,除非您明确要求它们,否则您通常不会看到它们,例如ps使用-m标志)。 This is caused by the fact that the Linux kernel doesn't really make much of a difference between threads and tasks.这是因为 Linux kernel 并没有真正在线程和任务之间产生太大差异。

Edit: as I just learned this is no longer necessarily the case: you can create a thread with the exact same PID as the parent, in which case the threads will be distinguished by different thread IDs.编辑:正如我刚刚了解到的,情况不再这样:您可以创建一个与父进程具有完全相同 PID 的线程,在这种情况下,线程将由不同的线程 ID 来区分。

However since a thread shares its memory with all other threads in the same process, this doesn't help you find out "how much memory a thread takes", since all threads in a process will use the exact same amount (and they all use the same, so the real used memory is shown_memory_use and not shown_memory_user * number_of_threads ).但是,由于一个线程与同一进程中的所有其他线程共享其 memory,这并不能帮助您找出“一个线程需要多少 memory”,因为一个进程中的所有线程将使用完全相同的数量(并且它们都使用一样,所以真正使用的 memory 是shown_memory_use而不是shown_memory_user * number_of_threads )。

Threads do not have PIDs, processes do.线程没有 PID,进程有。 As such what you're asking is not possible.因此,您的要求是不可能的。 There is also no reliable way to retrieve your PID from within a Java process (although the first part of the value returned by ManagementFactory.getRuntimeMXBean().getName() usually is the PID).也没有可靠的方法从 Java 进程中检索您的 PID(尽管ManagementFactory.getRuntimeMXBean().getName()返回的值的第一部分通常是 PID)。

As the name implies, PID means process ID.顾名思义,PID就是进程ID的意思。 Each process can spawn multiple threads, which all share the same PID.每个进程可以产生多个线程,它们都共享相同的 PID。 Are you sure you don't mean Thread ID?你确定你不是指线程ID?

A feature of thread is that is shares the heap with all other threads.线程的一个特点是与所有其他线程共享堆。 This means that any one thread can potentially use almost all the memory of the process.这意味着任何一个线程都可以潜在地使用该进程的几乎所有 memory。 The only thing which a thread doesn't have access to is the stack or local variables of another thread.线程唯一无法访问的是另一个线程的堆栈或局部变量。

As such it is not useful to try to determine how much memory an individual thread uses.因此,尝试确定单个线程使用多少 memory 是没有用的。 Instead it can be useful to determine how much memory a data structure uses.相反,确定数据结构使用多少 memory 可能很有用。 (Although this can have similar difficulties) (虽然这可能有类似的困难)

It is worth noting that main memory is relatively cheap.值得注意的是,主力memory比较便宜。 Your situation may be different but a typical new server with 24 GB can cost as little as £1K.您的情况可能有所不同,但一个典型的 24 GB 新服务器的成本可能低至 1000 英镑。 You can buy a 96 GB PC for around £2K.您可以花 2000 英镑左右购买一台 96 GB 的 PC。 Sometimes it is not worth worrying about how much memory you are using until you know it is a problem.有时不必担心您使用了多少 memory,直到您知道这是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM