Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is garbage collection done in Java and how does it compare to .net?

Tags:

java

.net

I am wondering what the differences are or if it is the same across both.

like image 925
Srikar Doddi Avatar asked May 28 '10 04:05

Srikar Doddi


People also ask

What is garbage collection How it is done in Java?

Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

How do you explain garbage collection?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

What type of garbage collection does Java use?

There are four types of the garbage collector in Java that can be used according to the requirement: Serial Garbage Collector. Parallel Garbage Collector. Concurrent Mark Sweep (CMS) Garbage Collector.

Why garbage collection is used in Java?

Java applications obtain objects in memory as needed. It is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses.


1 Answers

This question is not answerable.

Firstly, none of the relevant specifications will say anything about how Java or .net should implement garbage collection. So there is literally no "way that GC is done" in Java or in .net.

Secondly, the details of how GC is implemented differs between the various vendors for Java and .net respectively, and for any vendor the GC may change with each platform, each major version, minor version and even each patch version. On top of that, some implementations of Java allow you to choose between different garbage collectors using command line options.

Finally, it doesn't really matter how the GC is implemented in a Java or .net implementation provided that it can be made to work as required by the application. And for Java, the answer is that it can for most kinds of application. (The exceptions are typically systems/applications with hard real time requirements or very tight memory constraints.)

like image 184
Stephen C Avatar answered Oct 22 '22 19:10

Stephen C