Don't understand the volume of objects in java (java newbie confusion)

This question is based on the Scope of the chapter Thinking in Java, 2nd edition, p. 109 , and he says that when we create a Java object using a new one that hangs around, the end of the scope passes. He gives the following example:

{ String s = new String("a string"); } /* end of scope */ 

Then he says:

s link disappears at the end of the scope. However, the String object pointed to by s still takes up memory. In this bit of code, there is no access to the object, because only a reference to it is beyond the scope.

So, if I understand correctly, the text "string" is still present in memory, but the pointer that has the memory address of the first character "a" does not exist. Is that what it means?

Further,

It turns out because the objects created with the new one remain around as you want, a whole series of programming problems in C ++ simply disappear in Java.

Why is it profitable? In the above example, the string data continues to be in memory without the possibility of access to it (since the pointer was destroyed after it went out of scope), which only consumes resources.

+4
source share
7 answers

As soon as the Object goes beyond the scope , and it will be ready to collect garbage until the GC collects a separate object, it will occupy memory.

+2
source

Simply put, the scope of the reference variable s in the segment:

 { String s = new String("a string"); } /* end of scope */ 

located between braces. This means that s exists only between opening and closing {}. However, something happens in this block.

In curly braces, the new operator is called to instantiate the String object. Memory is allocated for the object, and a reference to it is assigned to the variable s .

At the end of the code block, s no longer exists, and therefore the link that it holds no longer exists. However, the String object still exists in memory. But since there is no longer any variable that holds a reference to it, it is no longer available for any part of the program. At this stage, the facility has the right to garbage collection.

As long as the object does not collect garbage, it still occupies a stain in the system memory, even if it is no longer available for the program.

+2
source

It’s good that it is stored in memory until the Java garbage collector cleans it for you. What does he do periodically. This means that you do not need to manage the memory yourself.

You can create objects without worrying about allocating and freeing memory.

+1
source

Since the object goes out of scope, it will be automatically garbage collected by the java garbage collector. This is beneficial then in C ++, because in java, the Java Runtime will automatically take care of garbage collection, so that eventually the memory will be restored. This does not apply to C ++

+1
source

I don’t understand the meaning the book is trying to make when it comes to blocks of code that still occupy memory. Perhaps what he is trying to say is that just because the reference variable (for example, s in the example) is out of scope does not mean that memory is immediately freed. What he cannot say (at least in what you sent) is that such memory is subject to automatic garbage collection and, in fact, you should program it as if such remaining blocks of memory would be collected.

The great thing about garbage collection in Java compared to C ++ is that all this is done automatically. When all strong references (including all indirect links) to memory allocation are beyond the scope, then this memory block is automatically garbage collected. What is this in a nutshell. Java handles things like circular references correctly, and garbage collects arbitrarily complex data structures if the structure is not directly or indirectly accessible from any reference value, still in scope.

In addition, there are special rules for strings due to interning , so the book type did not succeed with this example.

+1
source

In Java, when all references to an object have been nullified or fall out of scope, the object itself becomes a candidate for destruction by the garbage collector. GC does this automatically according to its own algorithm, whereas in C ++ you have to explicitly destroy the object. In addition, the string may continue to exist due to what is called internment.

+1
source

Go to page 215 to read about How the Garbage Collector Works .

While Java code is running, the Garbage Collector runs periodically to find all objects that are not links and free up the memory they occupy. When this happens, the finalize method is called on your object (if defined). At that time, you are doing any cleanup required by your facility.

With C ++, you have to worry about freeing memory yourself using the delete keyword - failure to do this can lead to memory leaks.

Using Java, you control the allocation of memory to control the life cycle of a managed entity.

+1
source

Source: https://habr.com/ru/post/1491366/


All Articles