SIZEOF in Java (1)

xiaoxiao2021-03-06  98

1. Java has an operator similar to the SIZEOF () in the C language? The surface answer is that Java does not provide an operator that is similar to the C language. However, we should think about why Java programmers need it. C language programmakers manage most data structural storage allocations, and sizeof () is not responsible for understanding the size of the allocated memory block. C Storage Distributor such as malloc (), as long as the object is initialized, almost everything: the programmer must set all the object domains of a deeper one-layer object pointer. But when all are said and encoded, C / C storage assignments are quite effective. In contrast, Java object allocation and construction is closely combined (it is not possible to use an object that has been assigned but no initialization). If the Java class defines a domain as a deeper layer object, it is also common in the construction phase. The Java Object Distributor thus frequently assigns interconnect objects: object graphics. All this is too convenient to collect, all this is too convenient, and you feel that you don't have to worry about the details of Java storage assignment. Of course, this is only valid for simple Java applications. As compared to C / C , the same Java data structure often occupies more physical storage. In business software development, close to the largest virtual storage on the 32-bit JVM today is a universal scalability limit. Therefore, Java programmers can benefit from SizeOf () or other similar functions because these functions can observe whether its data structure is too large or whether to include storage bottlenecks. Fortunately, Java reflections allow you to write this tool too much. Next, I will discuss several mistakes that often appear to this issue. Misunderstanding 1: Because of the size of the Java type, no sizeof () is good, Java Int is 32 digits on all JVMs and all platforms, but this is just a language specification requirement, and this data type can be accepted. Width. This int is basically an abstract data type and can be supported by a 64-bit memory word on a 64-bit device. Non-primary types are no exception: Java language norms do not involve such problems: How to calibrate or Boolean in the physical storage or Boolean can not be implemented as a simple bit vector in JVM. Misunderstanding 2: Serialize the object into a bit amount and then view the generated flux length to measure the size of the object. This method is invalid. This method is that the serial layout is just a remote reflection of the layout in the real memory. For example, by observing how string is serialized: at least 2 bytes per char in the memory, but in the serial format is UTF-8 encoding, any ASCII content only accounts for half of space. Another solution you might think of describing a trick in "Java Tip 130: Do you know your data size: On the basis of creating a large number of tag classes, the carefully measured in the stack size used in JVM growth of. If appropriate, this technique is quite useful. In fact, I also use it in this article to boot the alternate method. Note: The class SIZEO in Java Trusts requires a static JVM (such a stack activity can only be caused by the object assignment of the measurement thread request and the operation of the garbage collection, requiring a large number of object instances. If you want to measure a single large object (may be part of the debug track output) size, especially if you want to test what is actually what makes him become so big, this method is invalid. 2. What is the size of the object? The above discussion highlights a philosophical question: Suppose you often handle the object graphics, what is the definition of an object size? He refers to the size of the size of the object you are measuring or refers to the entire data graphic of the object instance? The latter uses more in actual life.

As you can see, things are not always divided so clearly, but for the launcher, you can refer to the following methods: • The sum of all non-static data fields (including the domain defined in the superclass) in the superclass is its Different size and C , class methods, and their virtual do not affect the size of the object. The hyper interface does not affect the object size (see the notes at the end of the list) • The complete object size can be closed as a closure of the entire object graphic rooted Get a comment: Implement any Java interface only to marks the doubt, and do not add any data to its definition. In fact, the JVM even does not check the interface to implement all the methods requested by the interface: In the current specification, this is strict to the compiler's responsibility. In order to guide the entire process, for the primary data type, I use the SizeOf class of Java Skills 130 to measure physical dimensions. As it is proven, for ordinary 32-bit JVM, a simple java.Lang.Object accounts for 8 bits, and basic data types are usually able to adapt to the least physical size of language requirements (except Boolean to occupy Outside the entire byte): // java.lang.Object shell size in bytes:

Public static final int object_shell_size = 8;

Public static final int jref_size = 4;

Public Static Final Int long_field_size = 8;

Public static final int INT_FIELD_SIZE = 4;

Public static final INT Short_field_size = 2;

Public static final int char_field_size = 2;

Public static final int Byte_field_size = 1;

Public static final int boolean_field_size = 1;

Public Static Final Int Double_field_size = 8;

Public static final int float_field_size = 4;

(These constants are not always hard coding, and for a given JVM, they must measure independently, realize this is important) Of course, childish calculation object domain dimensions often ignore the storage queue in JVM. The storage queue is really a matter (for example, the primary arrangement type in Java TIP 130), but I think it is useless in this low level detail. This detail is not only decided by the JVM developer, but they are also under the control of programmers. Our goal is to obtain the best estimate of the object size, and hope that excess in class domains, the domain should be simply assembled, or there is a need to be more compact embedded databases. For absolute physical accuracy, you can always return to the SizeOf class in Java TIP 130. To help the configuration files that make up the object instance, our tools don't just calculate the size, but also build a useful data structure: graphic consisting of IObjectProfilenode:

Interface IObjectProfilenode

{

Object object ();

String name ();

Int size ();

Int refcount ();

IOBJECTPROFILENODE PARENT ();

IObjectProfilenode [] Children ();

IObjectProfilenode shell (); IOBJECTPROFILENODE [] PATH ();

IOBJECTPROFILENODE ROOT ();

Int pathlength ();

Boolean Traverse (Inodefilter Filter, Inodevisitor Visitor);

String dump ();

} // end of interface

IObjectProfilenodes interconnects with a very similar method with the original object graphics, which uses the IObjectProfileNode.Object () function of the actual object represented by each node. IOBJECTPROFILENODE.SIZE () Returns the overall size (in bytes) of the object subtree with the object instance of the node. If the object instance is linked to other objects via a non-empty instance domain or by a reference included in the arranging domain, IOBJECTPROFILENODE.CHILDREN () will become a corresponding list of subtragraph nodes arranged in descending order. Conversely, IObjectProfilenode.Parent () returns their parent nods for each node that is not the start node. Thus the entire collection of iObjectProfilenode cuts off the original object and the display object is stored in its inside. Moreover, the graphics node is sided from the class domain, detects the node path within the graphic (IObjectProfilenode.path ()) allows you to retrore the ownership link from either of the original object instance to any part of the data. You may have noticed that some of the above paragraphs have a bit vague. If you encounter the same object in the traversal object (eg, how do you point to it in the graphic), how will you assign its ownership (parent pointer)? Consider the following code snippet:

Object obj = new string [] {New String ("javaworld"),

NEW STRING ("javaworld")}

Each java.lang.String instance has a type of CHAR [], the type char [] has a real string content. The String replication constructor is valid in the Java 2 platform standard (J2SE) 1.4, two String instances in the above arrangement share the same containing {'J', 'A', 'V', 'A', 'W ',' o ',' r ',' L ', the CHAR [] arrangement of the' D '} character sequence. Two strings are equal to this arrangement, so what do you do like this situation? If I always want to assign a single parent node to the graphic node, then this problem has no universally applicable answer. But in fact, many such object instances can be traced back to a single "natural" parent node. This natural link sequence is usually shorter than other more circulating paths. It is more dependent on the instance instead of this instance instead of other. Watch the items in the arrangement as more subordinate to this arrangement. Therefore, if the internal object instance can be reached through several roads, we choose the shortest road. If the path is as long as the path, we chose the first one that was discovered. In the worst case, this universal policy is useful. Consider the graphic traversal and shortest path should pay attention to this: Width priority search, this graphics traversal can ensure that the shortest path from the start node to any other reachable graphics node.

转载请注明原文地址:https://www.9cbs.com/read-123817.html

New Post(0)