What is the garbage collector in Java?

2019-01-01 02:08发布

I am new to Java and confused about the garbage collector in Java. What does it actually do and when does it comes into action. Please describe some of the properties of the garbage collector in Java.

16条回答
美炸的是我
2楼-- · 2019-01-01 02:53

Automatic garbage collection is a process where the JVM gets rid of or keeps certain data points in memory to ultimately free up space for the running program. Memory is first sent to heap memory, that is where the garbage collector (GC) does its work, then is decided to be terminated or kept. Java assumes that the programmer cannot always be trusted, so it terminates items it thinks it doesn't need.

查看更多
梦寄多情
3楼-- · 2019-01-01 02:54

garbage collector implies that objects that are no longer needed by the program are "garbage" and can be thrown away.

查看更多
栀子花@的思念
4楼-- · 2019-01-01 02:55

Many people think garbage collection collects and discards dead objects.
In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage.

When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system. To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm.

Check this for more detail information: http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx

查看更多
谁念西风独自凉
5楼-- · 2019-01-01 02:56

The garbage collector allows your computer to simulate a computer with infinite memory. The rest is just mechanism.

It does this by detecting when chunks of memory are no longer accessible from your code, and returning those chunks to the free store.

EDIT: Yes, the link is for C#, but C# and Java are identical in this regard.

查看更多
登录 后发表回答