Store "type Object pointer, synchronization block index" for C # CLR via object in-memory heap

Source: Internet
Author: User

Recently reading, I saw the way the object is stored in memory.

The object is stored in the memory heap, the allocated space in addition to the amount of RAM required by the members of the type object, as well as additional members (type Object pointers, synchronous block indexes), see this I am a little confused, do not know what type Object pointer is, what is pointing to?

From the Internet to find also did not find, the last look down, some description of the book. The following is my understanding:

Type Object pointer: The address that points to the type Object store, if there is a type person, it has an area in the heap that stores its internal fields and members, and two additional members (type Object pointers, synchronous block indexes), and the type object pointer to the System.Type address.

Because the person type exists as an object in memory relative to System.Type, the System.Type type is also a type object, and its type object pointer points to itself;

Instantiate a Person object, person P = new person (); The P object also allocates an area in the memory heap to store its internal fields and members, as well as two additional members (type Object pointers, synchronous block indexes), and P's type object pointer to the address of the person type in the heap.

Synchronization block Index: First, say the synchronization block. NET team in the design of the basic framework to fully consider the problem of thread synchronization, the result is. NET provides the ability to support thread synchronization for every object in the heap, which is the embryonic form of the synchronization mechanism "reference: http://genwoxuedotnet.blog.51cto.com/1852748/504107"

However, there is a big drawback to allocating synchronization blocks to each of the objects in the heap, which increases the memory consumption. In a typical system, objects that require a synchronization mechanism may only be small, so for most objects, the memory consumption of a synchronization block is completely wasted. In view of this,. NET Framework uses a compromise approach, which is to actually assign only one synchronous index to each object in the heap, which only holds an integer that indicates the index within the array. NET creates a new synchronous block array at load time, when an object needs to be synchronized. NET assigns it a synchronization block and adds the synchronization block's index in the synchronized block array to the synchronization block index of the object.

The synchronization block mechanism contains the following points:
· Initializes an array of synchronization blocks when. NET is loaded.
· Each object that is allocated on the heap contains two additional fields, one of which stores the type pointer, and the other is the synchronous block index, which is initially assigned a value of-1.
· When a thread attempts to use the object to enter synchronization, the synchronization index of the object is checked. If the index is negative, it looks for or creates a new synchronization block in the synchronized block array, and writes the index value of the synchronization block to the synchronization index of the object. If the object's synchronization index is not negative, locate the synchronization block for the object and check if any other threads are using the synchronization block, and if so, go to the wait state, and if not, declare the synchronization block to be used.

Synchronization blocks are referred to as. NET maintenance of an element in an array of synchronized blocks

Store "type Object pointer, synchronization block index" for C # CLR via object in-memory heap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.