Questions tagged [garbage-collection]

Questions about the automatic memory management used by virtual machines to ensure that unused memory/objects is released and reused.

wiki describes garbage collection as :

In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program.

109 questions
144
votes
10 answers

When is it a good idea to force garbage collection?

So I was reading a question about forcing the C# garbage collector to run where almost every single answer is the same: you can do it, but you shouldn't - except for some very rare cases. Sadly, nobody there elaborates on what are such cases. Can…
Saturn
  • 3,887
  • 9
  • 30
  • 40
102
votes
6 answers

Stack and Heap memory in Java

As I understand, in Java, stack memory holds primitives and method invocations and heap memory is used to store objects. Suppose I have a class class A { int a ; String b; //getters and setters } Where will the primitive a in…
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
87
votes
6 answers

How does garbage collection work in languages which are natively compiled?

After browsing several answers an Stack Overflow, it is clear that some natively compiled languages have garbage collection. But it is unclear to me how exactly this would work. I understand how garbage collection could work with an interpreted…
Christian Dean
  • 2,790
  • 1
  • 22
  • 38
86
votes
11 answers

Why Garbage Collection if smart pointers are there

These days, so many languages are garbage collected. It is even available for C++ by third parties. But C++ has RAII and smart pointers. So what's the point of using garbage collection? Is it doing something extra? And in other languages like C#, if…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
78
votes
11 answers

Why aren't Java objects deleted immediately after they are no longer referenced?

In Java, as soon as an object no longer has any references, it becomes eligible for deletion, but the JVM decides when the object is actually deleted. To use Objective-C terminology, all Java references are inherently "strong". However, in…
moonman239
  • 2,023
  • 4
  • 18
  • 23
70
votes
3 answers

How does garbage collection compare to reference counting?

I starting working through an online course on iOS development in the new language from Apple, Swift. The instructor made a point that raised this question in my mind. He said something to the effect of: There's no need to worry about memory…
Aaron Anodide
  • 5,463
  • 5
  • 28
  • 37
67
votes
16 answers

Why do languages such as C and C++ not have garbage collection, while Java does?

Well, I know that there are things like malloc/free for C, and new/using-a-destructor for memory management in C++, but I was wondering why there aren't "new updates" to these languages that allow the user to have the option to manually manage…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
61
votes
6 answers

When to use weak references in .Net?

I have not personally come across a situation where I've needed to use WeakReference type in .Net, but the popular belief seems to be that it should be used in caches. Dr Jon Harrop gave a very good case against the use of WeakReferences in caches…
theburningmonk
  • 721
  • 1
  • 5
  • 5
56
votes
5 answers

Why are reference-counting smart pointers so popular?

As I can see, smart pointers are used extensively in many real-world C++ projects. Though some kind of smart pointers are obviously beneficial to support RAII and ownership transfers, there is also a trend of using shared pointers by default, as a…
user99541
52
votes
8 answers

What happens to garbage in C++?

Java has an automatic GC that once in a while Stops The World, but takes care of garbage on a heap. Now C/C++ applications don't have these STW freezes, their memory usage doesn't grow infinitely either. How is this behavior achieved? How are the…
Ju Shua
  • 713
  • 1
  • 5
  • 8
44
votes
7 answers

Why is the finalize method included in Java?

According to this post, we should never rely on the finalize method to be called. So why did Java include it in the programming language at all? It seems like a terrible decision to include in any programming language a function that might get…
patstuart
  • 643
  • 5
  • 12
40
votes
8 answers

Disadvantages of scoped-based memory management

I really like scope-based memory management (SBMM), or RAII, as it is more commonly (confusingly?) referred to by the C++ community. As far as I know, except for C++ (and C), there's no other mainstream language in use today that makes SBMM/RAII…
Paul
  • 2,134
  • 3
  • 18
  • 18
38
votes
8 answers

Is overriding Object.finalize() really bad?

The main two arguments against overriding Object.finalize() is that: You don't get to decide when it's called. It may not get called at all. If I understand this correctly, I don't think those are good enough reasons to hate Object.finalize() so…
AxiomaticNexus
  • 776
  • 2
  • 7
  • 11
36
votes
3 answers

Do all functional languages use garbage collection?

Is there a functional language which allows to use stack semantics - automatic deterministic destruction at the end of the scope?
34
votes
7 answers

Does Garbage Collection Scan The Entire Memory?

I was reading a bit about garbage collectors and I am wondering if the garbage collector of a program scans the entire heap memory or what is allocated to it? If it reads the entire system memory, does it mean it is reading memory locations that are…
PoJam
  • 475
  • 4
  • 6
1
2 3 4 5 6 7 8