简体   繁体   English

传递给Java线程的对象是否占用堆外空间?

[英]Do objects passed to Java threads take up off-heap space?

I have an object like: 我有一个像这样的对象:

final List<Rows> rowsToSubmit = new ArrayList<>(rows);

And I instantiate threads as in: 我将线程实例化为:

// submit to a thread
executorService.submit(new Callable<Boolean>() {
  @Override
  public Boolean call() throws Exception {
    Object threadObj = new Object();
    return bq.doHttpPost(rowsToSubmit); // takes about 3 seconds for IO
  }
});

Does Java utilize the thread stack memory with the rowsToSubmit object as well? Java是否也将线程堆栈内存与rowsToSubmit对象一起使用? In other words do I need to increase my off-heap memory (I think Java thread stack resides there?) if my rowsToSubmit is huge? 换句话说,如果我的rowsToSubmit很大,是否需要增加堆外内存(我认为Java线程堆栈位于其中?)?

Also, is threadObj also initialized on the off-heap space? 另外, threadObj也在堆外空间上初始化?

All java objects are stored on the heap, so you shouldn't need to configure off-heap memory. 所有Java对象都存储在堆中,因此您无需配置堆外内存。

Java is a pass-by-reference language, meaning that objects are never duplicated between methods or threads. Java是一种按引用传递的语言,这意味着对象永远不会在方法或线程之间重复。 Only pointers are passed around, and the stack holds only primitives and pointers to objects. 仅传递指针,并且堆栈仅包含基元和指向对象的指针。 For example, while a pointer to the rowsToSubmit object is stored on the thread-stack, the memory for the contents of the rowsToSubmit object is still stored on the heap - the memory is shared between threads. 例如,当将rowsToSubmit对象的指针存储在线程堆栈上时, rowsToSubmit对象的内容的内存仍存储在堆中,该内存在线程之间共享。

Likewise, I wouldn't say that threadObj is initialized on the off-heap space because all java objects are on the heap. 同样,我不会说threadObj是在堆外空间上初始化的,因为所有java对象都在堆上。 The reference to threadObj is stored in the off-heap space. threadObj的引用存储在堆外空间中。

Further reading: http://www.journaldev.com/4098/java-heap-space-vs-stack-memory 进一步阅读: http : //www.journaldev.com/4098/java-heap-space-vs-stack-memory

Java Spec Java规范

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. 每个Java虚拟机线程都有一个私有Java虚拟机堆栈,与该线程同时创建。 A Java Virtual Machine stack stores frames (§2.6). Java虚拟机堆栈存储框架(第2.6节)。 A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Java虚拟机堆栈类似于常规语言(例如C)的堆栈:它保存局部变量和部分结果,并在方法调用和返回中起作用。 Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. 因为除了推送和弹出帧外,从不直接操纵Java虚拟机堆栈,所以可以为堆分配帧。 The memory for a Java Virtual Machine stack does not need to be contiguous. Java虚拟机堆栈的内存不必是连续的。

In The Java Virtual Machine Specification, First Edition, the Java Virtual Machine stack was known as the Java stack. 在Java虚拟机规范第一版中,Java虚拟机堆栈称为Java堆栈。

This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. 该规范允许Java虚拟机堆栈具有固定大小,或根据计算要求动态扩展和收缩。 If the Java Virtual Machine stacks are of a fixed size, the size of each Java Virtual Machine stack may be chosen independently when that stack is created. 如果Java虚拟机堆栈的大小固定,则在创建每个Java虚拟机堆栈时可以独立选择其大小。

Second when you pass an object through argument as in it is always pass by value.So in case it will not create any object in heap only copy the reference to the local variable and both will point to same object. 其次,当您通过实参传递对象时,它总是按值传递。因此,如果它不会在堆中创建任何对象,则仅将引用复制到局部变量,并且都将指向同一个对象。

Java utilizes the stack memory by access the pattern which makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Java通过访问模式来利用堆栈内存,这使得向其分配内存和取消分配内存变得微不足道(指针/整数只是简单地递增或递减),而堆的分配或释放则涉及更为复杂的簿记工作。

Each Thread in Java has their own stack which can be specified using -Xss JVM parameter, similarly, you can also specify heap size of Java program using JVM option -Xms and -Xmx where -Xms is starting size of the heap and -Xmx is a maximum size of java heap. Java中的每个线程都有自己的堆栈,可以使用-Xss JVM参数指定该堆栈,同样,您也可以使用JVM选项-Xms-Xmx指定Java程序的堆大小,其中-Xms是堆的开始大小,而-Xmx是Java堆的最大大小。

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

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