Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread model and class instance memory management

Assume we created an instance of a class in the stack. I understand that the compiler gives it a specific amount of memory depending on the types and amount of fields in that instance. But I am confused about the instance methods. I assume they have their own stack frame.

What I don't understand:

  1. Where the stack frame of the instance method is located? Are they located inside of the instance stack frame or they are stored elsewhere?
  2. Is there only one instance method stack frame created for many instances of the class
  3. if so, then what if two objects of the same class at the same time will call the same function from different threads?
like image 520
Lion Hanes Avatar asked Jul 21 '14 20:07

Lion Hanes


People also ask

What is the memory model for threads?

OpenMP provides a shared-memory model that allows all threads on a given device shared access to memory. For a given OpenMP region that may be executed by more than one thread or SIMD lane, variables in memory may be shared or private with respect to those threads or SIMD lanes.

What is the difference between PermGen and Metaspace?

PermGen always has a fixed maximum size. Metaspace by default auto increases its size depending on the underlying OS.

Which memory is used in threads Java?

2. Stack Memory in Java. Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects referred from the method that are in a heap.

What does Java's memory management do?

What is Memory Management In Java? Memory management in Java is the process of allocating working memory space to new objects and properly removing unreferenced objects to create space for those new object allocations.


2 Answers

Like normal functions, there are multiple pieces of memory associated with member functions in C++. First, there's the actual assembly instructions that make up the member function, which is usually put into the code segment and shouldn't be of any concern. Second, every time that member function is invoked, additional stack space is reserved for all of the local variables ("automatic objects") inside of that call, which is cleaned up when the call returns. I should specifically point out that functions don't have some fixed preallocated memory for their stack space - if a function is recursive, for example, you might have multiple stack frames active for that function at the same time. Rather, there's as many stack frames as needed.

When you declare a local variable of class type in C++, you only get the memory for the object itself. No extra memory is allocated to hold the member functions of that object - as mentioned above, the member function memory is either placed away in the data segment or is allocated as needed when you call the member functions. Specifically, if you call a member function on the object you've declared, then the program will allocate a new stack frame for that member function, call the function, and clean up the memory when the function returns. There is no extra "premium" paid for having member functions; they don't actually influence the size of the object (though having one or more virtual functions in your class may add a one-time cost to the size of your object).

Of course, this is all implementation-dependent; an implementation in principle could allocate extra space to store the member functions inside the object, but to the best of my knowledge no standard C++ implementations will do this. (If you know what a vtable is, the object might have a vtable pointer, but not all the vtable entries).

Hope this helps!

like image 59
templatetypedef Avatar answered Oct 21 '22 03:10

templatetypedef


1. Where the stack frame of the instance method is located? Are they located inside of the instance stack frame or they are stored elsewhere?

There's no such thing like an instance stack frame. Stack frames are created in the actual execution thread's call stack.

2. Is there only one instance method stack frame created for many instances of the class

See my answer for 1.. There are different call stacks per thread, yes.

3. if so, then what if two objects of the same class at the same time will call the same function from different threads?

As said before, there are different stack frames created for each thread. There aren't any call stack frames per instance. It's only the different implicitly passed this pointers, that distinguish the instances accessed.

like image 36
πάντα ῥεῖ Avatar answered Oct 21 '22 03:10

πάντα ῥεῖ