References and objects detailed in JAVA

  • 2020-06-07 04:32:22
  • OfStack

Thinking in Java 1 says that references and objects are like yao controllers and TV sets. Use the controller (reference) to operate the TV (object). If you want to change the channel, you can directly operate the controller. The controller then controls the TV. If you're walking around the room and you want to change channels on the TV, all you have to do is bring the controller. I think this is a good one, references are used to manipulate objects, objects are created by you. The new keyword is typically used to create 1 object. So how are these objects stored and how is memory allocated?

Where is it stored?

1. Registers (register) : Since the registers are inside CPU, they are the fastest, but the number is limited, so the compiler allocates them on demand.

2. Stack (stack) : in general RAM, the stack pointer moves to allocate and release memory, and the pointer moves downward to allocate new memory; Moving the pointer up frees memory. Speed is second only to registers. When creating a program, the Java compiler must know the exact size and lifetime of all the data stored on the stack, because it must generate the code to move the pointer up and down the stack, which limits the program's flexibility. So objects in java are not stored on the stack, but references to objects are.

3. Heap (heap) : Also a memory pool located in RAM for all JAVA objects. The compiler does not need to know how much storage to allocate from the heap, nor how long the stored data will live in the heap, so the heap is much more flexible than the stack. When new creates an object, the compiler automatically allocates storage in the heap. Of course, you have to code for this flexibility. Storage allocation with the heap takes more time than storage with the stack.

4. Static storage (static storage) : By "static" I mean "in a fixed position" (also in RAM). Static storage holds data that exists directly when the program is running. You can use the keyword static to identify that a particular element of an object is static, that is, a static member of a class, but the JAVA object itself is never stored in static storage.

5. Constant storage (constant storage) : Holds string constants and primitive type constants (public static final). Constant values are usually stored directly inside the program code, and they never change. Sometimes, in an embedded system, the constant itself is separated from the rest, so in that case, you can choose to put it in ROM.

Briefly describe the garbage collection mechanism

Garbage collection collects the memory space (heap) occupied by objects without any references, rather than the object itself. Note the following three points:

1) Objects may not be collected, that is, garbage collection will not be executed until 1;

2) Garbage collection does not equal destruction;

3) Garbage collection is only about memory.

Reference counters: A simple but slow garbage collection strategy. That is, each object has a reference counter, and the counter is incremented by 1 when a reference is connected to the object. The counter decreases by 1 when the reference leaves. The garbage collector iterates through the list of all objects, freeing up memory when it finds that an object has a reference counter of 0.

Advantage: The reference counting collector can be executed quickly, interwoven in the program run. A real-time environment in which the program is not interrupted for long periods of time is preferable.

Disadvantages: Circular references cannot be detected. If the parent object has a reference to a child object, the child object in turn references the parent object. Thus, their reference count can never be zero.

Adaptive, generational, stop-copy, mark-Sweep garbage collection:

Stop - Copy: Suspends the program and copies all living objects from the current heap to another heap. Anything that is not copied is garbage. When objects are copied from one heap to another, they are arranged one by one, so the new heap remains compact.

Tag - Sweep: Iterate over all references, find all living objects, and mark them. No objects are reclaimed, but cleanup begins when all of the tagging is done. Unmarked objects will be released without any replication, so the remaining heap space is not contiguous.

How many objects were created?

String s = "abc"; How many objects were created?

Of course, only one object was created -- "abc";

String s1 = "abc"; String s2 = s1; How many objects were created?

There is still only one object -- "abc";

String s1 = "abc"; String s2 abc = ""; How many objects were created?

There is still only one object -- "abc";

String s = "abc" + "def"; How many objects were created?

Notice that three objects are created: "abc"," def", "abcdef";

abc String s = new String (" "); How many objects were created?

You also know that there are two objects. In fact, "abc" is itself an object in the literal pool. When new String() is run, the string "abc" in pool is copied into the heap and the application of this object is handed to s, so two String objects are created, one in pool and one in the heap.

abc String s1 = new String (" "); abc String s2 = new String (" "); How many objects were created?

Three objects." abc" is one object in the literal pool, and then two objects are created in the heap with new String().


Related articles: