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:
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.
PermGen always has a fixed maximum size. Metaspace by default auto increases its size depending on the underlying OS.
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 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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With