diff --git "a/3ff48ac7-5b59-4f17-9c42-44a91f4bf04b.json" "b/3ff48ac7-5b59-4f17-9c42-44a91f4bf04b.json" new file mode 100644--- /dev/null +++ "b/3ff48ac7-5b59-4f17-9c42-44a91f4bf04b.json" @@ -0,0 +1,40 @@ +{ + "interaction_id": "3ff48ac7-5b59-4f17-9c42-44a91f4bf04b", + "search_results": [ + { + "page_name": "Creating a new language with Rust without Garbage Collection? - ...", + "page_url": "https://langdev.stackexchange.com/questions/2970/creating-a-new-language-with-rust-without-garbage-collection", + "page_snippet": "I am aware of Rust's ownership model that builds programs in a way that does not need garbage collection. So if I am to build a new programming language using safe Rust exclusively, will it be that the programming language will not need for me to implement garbage collection?I am aware of Rust's ownership model that builds programs in a way that does not need garbage collection. So if I am to build a new programming language using safe Rust exclusively, will it be that the programming language will not need for me to implement garbage collection? So if I am to build a new programming language using safe Rust exclusively, will it be that the programming language will not need for me to implement garbage collection? ... I want to create an interpreted language. I have decided to either use Nim or Rust.", + "page_result": "\n\n\n \n\n \n\n Creating a new language with Rust without Garbage Collection? - Programming Language Design and Implementation Stack Exchange\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n\n\n \n \n \n\n \n \n \n \n\n \n \n\n\n \n \n \n \n\n\n\n \n \n \n \n \n
\n
\n \n\n
\n\t
\n\t\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t
\n\t\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t

Stack Exchange Network

\n\t\t\t\t\t

\n\t\t\t\t\t\tStack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.\n\t\t\t\t\t

\n\t\t\t\t\tVisit Stack Exchange\n\t\t\t\t\t\n\t\t\t\t
\n\t\t\t
\n\n\n\n\n\n\t\t \n\t\t\n\n\n\n\t
\n
\n\n\t\n\n\n\n
\n \n\n\n
\n\n
\n
\n \nProgramming Language Design and Implementation Beta\n \n\n
\n
\n\n
\n \n\n
\n
\n \n
\n\n\n\n
\n
\n\n
\n

Teams

\n

Q&A for work

\n

Connect and share knowledge within a single location that is structured and easy to search.

\n \n Learn more about Teams\n \n
\n\n
\n \n
\n
\n\n\n\n
\n\n\n\n
\n\n \n\n
\n \n\n
\n\n \n\n \n
\n
\n Asked\n \n
\n
\n Modified\n 3 months ago\n
\n
\n Viewed\n 3k times\n
\n
\n\n
\n \n
\n\n\n
\n
\n
\n \n
\n 5\n
\n \n\n\n \n\n\n\n\n\n\n\n\n\n \n \n\n
\n\n
\n\n \n\n
\n $\\begingroup$\n
\n \n

I am new to language development. I am aware of Rust's ownership model that builds programs in a way that does not need garbage collection.

\n

So if I am to build a new programming language using safe Rust exclusively, will it be that the programming language will not need for me to implement garbage collection?

\n

Here is the context;

\n

I want to create an interpreted language. I have decided to either use Nim or Rust.

\n

I love Nim's syntax and find it fun to work with. I also hear interesting things concerning Rust, especially regarding its safety features.

\n

So my questions are;

\n
    \n
  1. Does using Rust eliminate the need to implement garbage collection in a language? If not, what other benefits do you think one can get by using Rust for working on an interpreted language?

    \n
  2. \n
  3. If I am to use Nim, what exactly will I be losing (or gaining) over rust?

    \n
  4. \n
\n
\n\n \n\n
\n
\n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n\n
\n
\n \n
\n
\"Ginger's
\n
\n
\n Ginger\n
\n 2,57977 silver badges4545 bronze badges\n
\n
\n
\n
\n
\n
\n
\n
\n asked Sep 27, 2023 at 17:51\n
\n \n
\n
\n
\"M4X_'s
\n
\n
\n M4X_M4X_\n
\n 15311 silver badge55 bronze badges\n
\n
\n
\n\n\n
\n
\n
\n $\\endgroup$\n
\n\n\n\n\n 6 \n
\n
\n
    \n\n
  • \n
    \n
    \n 17\n
    \n
    \n
    \n
    \n $\\begingroup$\n If you use Rust to implement a different language, then the fact that Rust - the host language - does not use a GC, does not imply anything about whether the language you are implementing will have a GC or not. That's a language design decision for you, the language designer, to make.\n $\\endgroup$\n
    \n– kaya3\n
    \n Sep 27, 2023 at 18:00\n
    \n
    \n
  • \n
  • \n
    \n
    \n 4\n
    \n
    \n
    \n
    \n $\\begingroup$\n The top question here about GC is answerable, but the two labelled \u201cmy questions\u201d are different and very broad currently. Which part of this are you looking for an answer to here? You can ask separate questions for different topics, although the broad ones would need to be dialed in a bit more for people to be able to give you good answers.\n $\\endgroup$\n
    \n– Michael Homer\n
    \n Sep 27, 2023 at 18:08\n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n $\\begingroup$\n Welcome to PLDI! One thing worth noting is that the things you hear about Rust regarding safety are typically relative to lower level languages like C/C++. Nim is likely to be similar in how safe it is (I don't know much about it tho).\n $\\endgroup$\n \n Sep 27, 2023 at 18:09\n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n $\\begingroup$\n Nim has a GC (you can choose from multiple GCs, actually, or no GC if you want), so if you make an interpreter in Nim and don't do any unsafe stuff, you shouldn't need to implement garbage collection yourself. I'm not sure why you mentioned Nim and Rust specifically, because any language that has either a GC or borrow checking like Rust can be used to implement an interpreter without requiring another GC, as long as you don't do something unsafe that the GC or borrow checker don't know about/can't handle.\n $\\endgroup$\n
    \n– user\n
    \n Sep 27, 2023 at 18:17\n
    \n
    \n
  • \n
  • \n
    \n
    \n 3\n
    \n
    \n
    \n
    \n $\\begingroup$\n You should think about whether your language implementation is as an interpreter or with a compiler \u2014 you didn't say, and it can make a difference.\n $\\endgroup$\n
    \n– Erik Eidt\n
    \n Sep 27, 2023 at 19:01\n \n \n \n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n\n
\n\n\n\n \n \n
\n \n
\n
\n
\n

\n 4 Answers\n 4\n

\n
\n
\n \n\n
\n
\n \n \n Reset to default\n \n
\n
\n \n
\n
\n\n\n
\n
\n
\n\n\n \n\n
\n
\n
\n
\n \n
\n 17\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n $\\begingroup$\n
\n
\n

Does using Rust eliminate the need to implement garbage collection in a language?

\n
\n

No. Rust\u2019s memory management relies on semantic properties of the language that are carefully guaranteed. Unless your language is implemented as an embedded domain-specific language within Rust (that is, it is \u201cjust\u201d a Rust library, perhaps with some fancy macros to provide sugar), you cannot easily inherit these guarantees from the host language, so you will still have to worry about your language\u2019s memory management in the usual way.

\n

If your language is interpreted, and you wish to remain within the safe subset of Rust, you will somehow have to prove to the borrow checker that values of your language have well-defined lifetimes. Depending on the semantics of your language, this may be tricky to do. On the other hand, if your language is compiled, Rust\u2019s memory management has little impact on the memory and lifetime semantics of your language, as you are free to produce whatever code you\u2019d like.

\n

If your language is intended to support fully automatic memory management without explicit lifetimes, then an interpreter is likely to be easier to implement in a garbage collected language, as it is possible to reuse the garbage collector of the host language in that case. However, again, if your language is compiled, the memory management of the host language is essentially irrelevant.

\n
\n

If not what other benefits do u think one can get by using Rust for working on an interpreted language. [\u2026] If I am to use Nim what exactly will I be missing out (or benefiting) on if I choose to use Nim or Rust to implement a language.

\n
\n

These questions are not on-topic on this site, as they are extremely broad and in some cases essentially come down to opinion. Also, more broadly, it\u2019s best to only ask one question per question on Stack Exchange sites. See our Help Center for what types of questions you can ask here.

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Sep 27, 2023 at 18:13\n
\n \n
\n
\n
\"Alexis
\n
\n
\n Alexis KingAlexis King\n
\n 8,02666 gold badges3232 silver badges6161 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n $\\endgroup$\n
\n\n\n\n\n 5 \n
\n
\n
    \n\n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n Thank you very much. This was helpful.\n $\\endgroup$\n
    \n– M4X_\n
    \n Sep 27, 2023 at 18:19\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n In short (and assuming that my own understanding is correct), simple data types like integers will be deallocated once the the semantics determine they will no longer be used, while more complex types like strings are garbage collected. It is this semantic analysis, as distinct from a simple consideration of scope etc., which distinguishes Rust from its ALGOL-based predecessors.\n $\\endgroup$\n \n Sep 28, 2023 at 7:06\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n @MarkMorganLloyd describing rust as Garbage Collected is a bit iffy. Most common understandings of garbage collection are about scanning collectors, which rust does not have. Rusts analysis falls under the same bucket as Objective C++ ARC, and RAII. Semantic lifetime analysis which is different enough to be regarded as seperate.\n $\\endgroup$\n
    \n– user1937198\n
    \n Sep 28, 2023 at 8:48\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n @user1937198 I agree, but I was trying to to stray too far from OP's phrasing. Also I think it's fair- in the general case- to assume that implementations can use different classes of storage one of which might be heap for strings etc.\n $\\endgroup$\n \n Sep 28, 2023 at 8:51\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n @MarkMorganLloyd An interpreter might. Rust as it currently stands? Only one heap in the standard library. Any alternative allocation store would be a custom library. Whether the rust language recognizes the existence of heap at all is a bit iffy, boxes etc are part of alloc, not core, and running Rust with just core is a standard supported configuration, unlike say C where the standard specifies malloc will exist because the standard only specifies the full stdlib.\n $\\endgroup$\n
    \n– user1937198\n
    \n Sep 28, 2023 at 8:56\n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n \n\n
\n
\n
\n
\n \n
\n 2\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n $\\begingroup$\n
\n

No, while rust's safety model will prevent many types of memory errors like use-after-free, it does not prevent memory leaks. From the Rust book:

\n
\n

Rust\u2019s memory safety guarantees make it difficult, but not impossible, to accidentally create memory that is never cleaned up (known as a memory leak). Preventing memory leaks entirely is not one of Rust\u2019s guarantees in the same way that disallowing data races at compile time is, meaning memory leaks are memory safe in Rust. We can see that Rust allows memory leaks by using Rc and RefCell: it\u2019s possible to create references where items refer to each other in a cycle. This creates memory leaks because the reference count of each item in the cycle will never reach 0, and the values will never be dropped.

\n
\n

You can trivially create a memory leak by creating a cycle with an Arc<T>/Rc<T>. Also functions like std::mem::forget are considered safe even though they may leak memory.

\n

A language implemented only in safe rust will inherit rust's safety guarantees, but will still leak memory as that isn't one of the guarentees. If you want memory usage to stay within limits you need some system to detect leaked objects and free them (eg. a Mark and Sweep Garbage Collector)

\n

You don't need a garbage collector though, if you are willing to accept leaks can happen and are not much of a problem. PHP for example uses only reference counting to free objects which means it will leak memory in much the same situations as rust would. If you are a language intended to be used for short scripts it may not be a huge problem.

\n

Further reading:

\n\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n
\n
\n \n
\n \n
\n
\n \n
\n \n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Sep 28, 2023 at 7:43\n
\n \n
\n
\n
\"mousetail's
\n
\n
\n mousetailmousetail\n
\n 8,1112222 silver badges6363 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n $\\endgroup$\n
\n\n\n\n\n 1 \n
\n
\n
    \n\n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n PHP does not leak memory with circular references; it keeps a list of freed references, and periodically runs a specific garbage collection algorithm to detect if any lead to closed cycles. This has been enabled by default since PHP 5.3, released in 2009. See php.net/manual/en/features.gc.collecting-cycles.php\n $\\endgroup$\n
    \n– IMSoP\n
    \n Oct 28, 2023 at 13:38\n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n \n\n
\n
\n
\n
\n \n
\n 1\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n $\\begingroup$\n
\n
\n

Does using Rust eliminate the need to implement garbage collection in a language?

\n
\n

No. The language in which a language is implemented has almost nothing to do with the properties of the implemented language. Rust was originally written in OCaml, for example, but OCaml is garbage collected and Rust is not.

\n
\n

If I am to use Nim, what exactly will I be losing (or gaining) over Rust?

\n
\n

As you noted, Nim is garbage collected (reference counted) and does not use an ownership system for anything other than eliding reference counts. There are some data structures that are difficult to represent in an ownership system like Rust, notably trees. If you use a tree to represent any part of your language - and you likely will - it's probable that you will run into difficulties with the borrow checker, especially starting off, or have to fall back on using Rc and Arc to form a reference-counted structure. (a possible alternative is to instead represent trees as Vecs)

\n

Rust is descended from the ML family of languages, however, and inherited many of their features that make them nice for building languages (ML: literally "Meta Language"). The combination of Rust's struct and enum types allow for the modelling of many data structures useful for implementing a language, while its match statement allows for structurally decomposing them. These are heavily inspired by ML's data/type/datatype and match/case/of constructs.

\n

Rust's struct is directly equivalent to Nim's object. Rust's enum has no direct equivalent. Instead, Nim has "object variants": structs that have a tag that can be matched upon to access different fields. This means that fields can be shared across variants, unlike Rust's enum, but the tag must be explicitly defined separately. This is generally a little harder to work with. Nim also does not have structural pattern matching: its case/of statement can only match upon ordinal types and strings (though, there are libraries that extend it to work structurally).

\n

There's not a right or wrong choice, of course, and there are many examples of languages implemented in both, but hopefully that provided some exposition as to what language features make a language suitable for implementing other languages in.

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Oct 26, 2023 at 12:53\n
\n \n
\n
\n
\"apropos's
\n
\n
\n aproposapropos\n
\n 95233 silver badges1414 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n $\\endgroup$\n
\n\n\n\n\n 3 \n
\n
\n
    \n\n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n "there are many examples of languages implemented in both" have people have written a lot of compilers in Nim? Actually while I'm less surprised about Rust for all the reasons you name I'd love for you to link some of the Rust examples as well.\n $\\endgroup$\n
    \n– Jared Smith\n
    \n Oct 27, 2023 at 12:45\n
    \n
    \n
  • \n
  • \n
    \n
    \n 2\n
    \n
    \n
    \n
    \n $\\begingroup$\n Yes! Here are a couple: github.com/arturo-lang/arturo, github.com/h3rald/min, github.com/calebwin/pipelines. Plus the Nim compiler itself and community libraries of a certain complexity, ex. the LLVM backend. As for Rust, there's a neatly collected (and long) list at github.com/alilleybrinker/langs-in-rust.\n $\\endgroup$\n
    \n– apropos\n
    \n Oct 28, 2023 at 11:02\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n $\\begingroup$\n Thanks! This is why I love the internet :)\n $\\endgroup$\n
    \n– Jared Smith\n
    \n Oct 28, 2023 at 12:22\n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n \n\n
\n
\n
\n
\n \n
\n 1\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n $\\begingroup$\n
\n

Rust does not provide garbage collection out of the box. But when you write a new language in Rust, the new language may require garbage collection. You can not write a Scheme interpreter without implementing a garbage collector. Whether you need a garbage collector depends on the language you want to implement. Rust does not require one, but other languages require one. And if the new language requires garbage collection, Rust's borrow semantics will not help. You have to write a garbage collector in Rust in the same way people wrote garbage collectors in C.

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Nov 8, 2023 at 20:06\n
\n \n
\n
\n
\"ceving's
\n
\n
\n cevingceving\n
\n 35611 silver badge99 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n $\\endgroup$\n
\n\n\n\n\n \n
\n
\n
    \n\n
\n\t
\n\n \n
\n
\n
\n\n

\n You must log in to answer this question.\n

\n\n\n\n

\n
\nNot the answer you're looking for? Browse other questions tagged .
\n

\n
\n
\n\n \n
\n \n\n \n \n\n\n \n\n \n\n\n \n \n\n\n\n
\n

\n \n Hot Network Questions\n \n

\n \n\n \n more hot questions\n \n
\n\n \n \n\n
\n\n
\n\n
lang-rust
\n\n \n\n
\n
\n\n \n \n\n\n \n\n \n \n \n \n \n \n\n\n \n \n \n", + "page_last_modified": "" + }, + { + "page_name": "GitHub - Manishearth/rust-gc: Simple tracing (mark and sweep) garbage ...", + "page_url": "https://github.com/Manishearth/rust-gc", + "page_snippet": "Simple tracing (mark and sweep) garbage collector for Rust - GitHub - Manishearth/rust-gc: Simple tracing (mark and sweep) garbage collector for RustSimple tracing (mark and sweep) garbage collector for Rust - GitHub - Manishearth/rust-gc: Simple tracing (mark and sweep) garbage collector for Rust The current GC is not concurrent and the GCed objects are confined to a thread. There is an experimental concurrent collector in this pull request. While this can be used pervasively, this is intended to be used only when needed, following Rust's \"pay only for what you need\" model. Avoid using Gc where Rc or Box would be equally usable.", + "page_result": "\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n \n \n \n \n \n\n \n\n \n \n \n \n \n \n\n\n \n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n GitHub - Manishearth/rust-gc: Simple tracing (mark and sweep) garbage collector for Rust\n\n\n\n \n \n \n\n \n \n\n\n \n\n\n \n\n\n \n \n\n \n \n\n \n \n \n \n \n\n\n\n \n\n \n\n\n\n\n \n\n \n\n \n\n \n \n \n \n \n \n \n \n \n \n\n\n\n \n\n\n\n \n\n\n \n \n \n \n\n \n\n \n\n \n\n \n\n\n\n \n \n\n\n \n\n \n\n \n \n \n\n\n\n\n\n \n\n \n\n \n
\n \n\n\n
\n Skip to content\n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\n\n \n \n
\n\n\n\n\n \n\n \n\n \n\n\n
\n \n\n
\n
\n \n \n \n\n \n\n \n\n
\n \n
\n
\n\n\n
\n
\n \n\n
\n \n\n\n\">\n \n \n
\n \n \n\n
\n Search or jump to...\n
\n \n\n
\n \n\n \n\n \n
\n \n

Search code, repositories, users, issues, pull requests...

\n
\n \n
\n
\n \n
\n \n \n \n \n \n\n \n
\n
\n
\n
\n \n
\n
\n Clear\n \n\n
\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n \n
\n
\n
\n\n \n
\n
\n\n
\n
\n
\n \n
\n \n\n \n
\n
\n
\n

\n Provide feedback\n

\n
\n
\n \n
\n
\n
\n \n
\n

We read every piece of feedback, and take your input very seriously.

\n \n \n \n
\n
\n \n
\n\n \n \n\n \n
\n
\n
\n

\n Saved searches\n

\n

Use saved searches to filter your results more quickly

\n
\n
\n \n
\n
\n
\n \n
\n\n \n\n
\n
\n
\n\n
\n
\n \n
\n
\n
\n\n\n\n \n\n \n Sign up\n \n
\n
\n
\n \n\n\n \n \n\n
\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n \n
\n\n\n \n \n\n\n\n\n\n\n \n
\n
\n \n \n\n\n\n\n\n \n\n\n\n\n\n\n \n
\n\n
\n\n
\n \n
\n \n \n\n \n \n \n Manishearth\n \n /\n \n rust-gc\n \n\n Public\n
\n\n\n
\n\n
\n \n\n
\n
\n\n
\n
\n

\n Simple tracing (mark and sweep) garbage collector for Rust\n

\n\n

License

\n \n\n\n \n\n
\n
\n \n
\n \n \n \n\n \n
\n
\n\n
\n\n\n \n\n
\n\n \n\n\n\n\n
\n \n\n\n \n \n

Manishearth/rust-gc

\n
\n
\n\n \n\n \n\n
\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n

Folders and files

NameName
Last commit message
Last commit date

Latest commit

\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0
\u00a0

Repository files navigation

rust-gc

\n

\"Build

\n

Simple tracing (mark and sweep) garbage collector for Rust

\n

The design and motivation is illustrated in this blog post, with a sketch of the code in this gist.

\n

There is another post about the initial design of cgc, its experimental concurrent branch.

\n

How to use

\n

To include in your project, add the following to your Cargo.toml:

\n
[dependencies]\ngc = { version = \"0.5\", features = [\"derive\"] }
\n

This can be used pretty much like Rc, with the exception of interior mutability.

\n

While this can be used pervasively, this is intended to be used only when needed, following Rust's \"pay only for what you need\" model. Avoid using Gc where Rc or Box would be equally usable.

\n

Types placed inside a Gc must implement Trace and Finalize. The easiest way to do this is to use the gc_derive crate:

\n
use gc::{Finalize, Gc, Trace};\n\n#[derive(Trace, Finalize)]\nstruct Foo {\n    x: Option<Gc<Foo>>,\n    y: u8,\n    // ...\n}\n\n// now, `Gc<Foo>` may be used
\n

Finalize may also be implemented directly on the struct, in order to add custom finalizer behavior:

\n
use gc::{Finalize, Trace};\n\n#[derive(Trace)]\nstruct Foo {...}\n\nimpl Finalize for Foo {\n    fn finalize(&self) {\n        // Clean up resources for Foo, because we think it will be destroyed.\n        // Foo may not be destroyed after a call to finalize, as another\n        // finalizer may create a reference to it due to reference cycles.\n    }\n}
\n

For types defined in the stdlib, please file an issue on this repository (use the unsafe_ignore_trace method shown below to make things work in the meantime).

\n

Note that Trace is only needed for types which transitively contain a Gc, if you are sure that this isn't the case, you may use the unsafe_empty_trace! macro on your types. Alternatively, use the #[unsafe_ignore_trace] annotation on the struct field. Incorrect usage of unsafe_empty_trace and unsafe_ignore_trace may lead to unsafety.

\n
use gc::{Finalize, Gc, Trace};\nuse bar::Baz;\n\n#[derive(Trace, Finalize)]\nstruct Foo {\n    x: Option<Gc<Foo>>,\n    #[unsafe_ignore_trace]\n    y: Baz, // we are assuming that `Baz` doesn't contain any `Gc` objects\n    // ...\n}
\n

To use Gc, simply call Gc::new:

\n
let x = Gc::new(1_u8);\nlet y = Gc::new(Box::new(Gc::new(1_u8)));\n\n#[derive(Trace, Finalize)]\nstruct Foo {\n    a: Gc<u8>,\n    b: u8\n}\n\nlet z = Gc::new(Foo {a: x.clone(), b: 1})
\n

Calling clone() on a Gc will create another garbage collected reference to the same object. For the most part, try to use borrowed references to the inner value instead of cloning the Gc wherever possible -- Gc implements Deref and is compatible with borrowing.

\n

Gc is an immutable container. Much like with Rc, to get mutability, we must use a cell type. The regular RefCell from the stdlib will not work with Gc (as it does not implement Trace), instead, use GcCell. GcCell behaves very similar to RefCell, except that it internally helps keep track of GC roots.

\n
#[derive(Trace, Finalize)]\nstruct Foo {\n    cyclic: GcCell<Option<Gc<Foo>>>,\n    data: u8,\n}\n\nlet foo1 = Gc::new(Foo {cyclic: GcCell::new(None), data: 1});\nlet foo2 = Gc::new(Foo {cyclic: GcCell::new(Some(foo1.clone())), data: 2});\nlet foo3 = Gc::new(Foo {cyclic: GcCell::new(Some(foo2.clone())), data: 3});\n*foo1.cyclic.borrow_mut() = Some(foo3.clone());
\n

Known issues

\n
    \n
  • Destructors should not access Gc/GcCell values. This is enforced by the Trace custom derive automatically implementing Drop with a safe empty drop method. Finalize should be used for cleanup instead.
  • \n
  • There needs to be a better story for cross-crate deriving.
  • \n
  • The current GC is not concurrent and the GCed objects are confined to a thread. There is an experimental concurrent collector in this pull request.
  • \n
\n

Related projects

\n
    \n
  • RuScript: Uses single-thread rust-gc to allocate memory for various objects
  • \n
\n
\n\n\n \n
\n
\n\n
\n
\n
\n
\n

About

\n\n

\n Simple tracing (mark and sweep) garbage collector for Rust\n

\n\n\n

Resources

\n \n\n

License

\n \n\n\n\n\n \n \n\n \n\n\n

Stars

\n \n\n

Watchers

\n \n\n

Forks

\n \n\n \n
\n\n
\n
\n\n \n \n\n \n \n
\n
\n

\n \n Packages\n \n

\n\n\n
\n No packages published
\n
\n\n\n\n
\n
\n\n \n \n\n \n
\n
\n

\n \n Contributors\n 28\n

\n\n\n \n
    \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
  • \n
    \n
  • \n
\n
\n\n \n
\n
\n\n \n \n
\n
\n

Languages

\n
\n \n \n \n
\n\n\n
\n
\n\n
\n
\n \n
\n\n
\n\n\n
\n\n
\n\n\n
\n
\n\n \n\n
\n

Footer

\n\n \n\n\n
\n
\n \n \n \n\n\n \n © 2024 GitHub, Inc.\n \n
\n\n \n
\n
\n\n\n\n\n \n\n\n \n\n \n\n
\n
\n
\n
\n\n \n\n\n\n\n\n \n\n
\n
\n \n\n\n", + "page_last_modified": "" + }, + { + "page_name": "Rust garbage container", + "page_url": "https://www.educative.io/answers/rust-garbage-container", + "page_snippet": "Design considerations and trade-offs in Rust: A garbage collector was excluded from the language's essential features because of Rust's emphasis on systems programming. The language does not have a garbage collector since it prioritizes control, memory safety, and performance.Design considerations and trade-offs in Rust: A garbage collector was excluded from the language's essential features because of Rust's emphasis on systems programming. The language does not have a garbage collector since it prioritizes control, memory safety, and performance. Removing non-deterministic behavior and runtime overhead: Rust reduces the runtime overhead related to garbage collection by not using a garbage collector. Rust is excellent for dependable and effective systems programming because of its zero-cost abstractions and effective memory management. Design considerations and trade-offs in Rust: A garbage collector was excluded from the language's essential features because of Rust's emphasis on systems programming. It offers Rust garbage-collected arenas and a way to interact with them safely. shredder: It is a library for the Rust programming language that offers a garbage-collected smart pointer, or gc. While Rc is beneficial in the same scenarios, this smart pointer can handle reference cycles. It can take non-static data and operate reliably in parallel programs. Bronze: For Rust, this is a supplemental library-based garbage collector. By tackling the difficulties that users encounter when utilizing Rust's ownership, borrowing, and lifetimes features, it was created to make the language easier to use.", + "page_result": "Educative Answers - Trusted Answers to Developer Questions
Trusted answers to developer questions

Rust garbage container

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you\u2019d learn in a bootcamp or university \u2014 at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

\n

A systems programming language called Rust combines memory safety principles with contemporary language features to produce performance comparable to low-level languages like C and C++. Mozilla created it, and has grown in popularity because of its propensity for making quick, dependable, and secure code.

Memory management in Rust

  • Memory safety is prioritized in Rust to shield against frequent programming mistakes and security flaws.

  • By enforcing rigid guidelines at compile time, its ownership and borrowing mechanism avoids problems like data races and null pointer dereferences.

  • Rust prioritizes performance in addition to memory safety, enabling low-level control while preserving effectiveness.

  • Rust is excellent for dependable and effective systems programming because of its zero-cost abstractions and effective memory management.

Lack of garbage collector in Rust

  • Design considerations and trade-offs in Rust: A garbage collector was excluded from the language's essential features because of Rust's emphasis on systems programming. The language does not have a garbage collector since it prioritizes control, memory safety, and performance.

  • Removing non-deterministic behavior and runtime overhead: Rust reduces the runtime overhead related to garbage collection by not using a garbage collector. The predicted resource utilization ensured by deterministic memory deallocation prevents non-deterministic pauses brought on by garbage collection.

  • Memory safety guarantees at compile time: The borrow checker in Rust ensures memory safety at compilation time. The borrow checker examines the code to check for valid memory accesses and avoid typical memory-related mistakes like dangling pointers or use-after-free issues. With the use of static analysis, runtime garbage collection is not necessary to ensure memory safety.

Rust garbage collection libraries

Even though there's no specific need for a garbage collector in Rust, we can make our own. Several Rust libraries offer garbage collection functionality. These libraries comprise, among others:

  • rust-gc: Rust's built-in garbage collector, rust-gc, is a straightforward tracing (mark and sweep) system. It can be used almost exactly like Rc, except for interior mutability. Types inserted into a gc are required to use Trace and Finalise. Using the gc_derive crate is the simplest method to accomplish this.

  • gc-arena: This Rust garbage collection mechanism is experimental.
    It offers Rust garbage-collected arenas and a way to interact with them safely.

  • shredder: It is a library for the Rust programming language that offers a garbage-collected smart pointer, or gc. While Rc is beneficial in the same scenarios, this smart pointer can handle reference cycles. It can take non-static data and operate reliably in parallel programs.

  • Bronze: For Rust, this is a supplemental library-based garbage collector.
    By tackling the difficulties that users encounter when utilizing Rust's ownership, borrowing, and lifetimes features, it was created to make the language easier to use. However, Rust's default memory management technique is the most popular method, and Bronze is not generally adopted.

Conclusion

The memory management strategy Rust uses emphasizes ownership rather than borrowing, disregarding the necessity for a garbage collector. Compile-time guarantees, deterministic deallocation, and effective resource management result from its emphasis on memory safety and performance.

For heap-allocated items and smart pointers for shared ownership, Rust offers techniques like Box<T>. Rust's Foreign Function Interface (FFI) enables smooth integration with external libraries. There is no particular need for garbage collection, but it does have libraries that support it. Rust's memory management strategy ensures dependability, effectiveness, and safe memory handling, making it ideally suited for systems programming overall.

RELATED TAGS

rust
garbage collector
container

CONTRIBUTOR

Khansa Shahid
Copyright \u00a92024 Educative, Inc. All rights reserved
Keep Exploring
Related Courses
Did you find this helpful?
\n", + "page_last_modified": "" + }, + { + "page_name": "memory - What does Rust have instead of a garbage collector? - ...", + "page_url": "https://stackoverflow.com/questions/32677420/what-does-rust-have-instead-of-a-garbage-collector", + "page_snippet": "Rust is garbage collected, like any other practical programming language. It's just that everybody thinks about garbage collection the wrong way. ... Garbage collection is typically used periodically or on demand, like if the heap is close to full or above some threshold.Rust is garbage collected, like any other practical programming language. It's just that everybody thinks about garbage collection the wrong way. ... Garbage collection is typically used periodically or on demand, like if the heap is close to full or above some threshold. It then looks for unused variables and frees their memory, depending on the algorithm. Rust would know when the variable gets out of scope or its lifetime ends at compile time and thus insert the corresponding LLVM/assembly instructions to free the memory. The basic idea of managing resources (including memory) in a program, whatever the strategy, is that the resources tied to unreachable \"objects\" can be reclaimed. Beyond memory, those resources can be mutex locks, file handles, sockets, database connections... Languages with a garbage collector periodically scan the memory (one way or another) to find unused objects, release the resources associated with them, and finally release the memory used by those objects. ... Rust has ownership. It knows when the program is using memory and immediately frees the memory once it is no longer needed. It enforces memory rules at compile time, making it virtually impossible to have runtime memory bugs. You do not need to manually keep track of memory. The compiler takes care of it. Discord recently switched from Go to Rust in one of its services just because garbage collector was causing latency. Abstractly, we normally consider langauges like C++/Rust that use RAII/RC as non-garbage-collecting. And languages such as Java/Python/C# to be garbage collecting (Even if it uses RC as an underlying implementation). The core difference is that in C++/Rust, the RC is explicit, and it's virtually a 5-line wrapper around calling malloc and free yourself.", + "page_result": "\n\n\n \n\n \n\n memory - What does Rust have instead of a garbage collector? - Stack Overflow\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n\n\n \n \n \n\n \n \n \n \n\n \n \n\n\n \n\n\n\n \n \n \n \n \n
\n
\n \n\n
\n\t
\n\t\t\t\n\t\t\t
\n\t\t\t\t
\n\t\t\t
\n\t\t\t\t\n\n\n\n\t\t\t
    \n\n\t\t\t\t\t
  1. \n\t\t\t\t\t\tAbout\n\t\t\t\t\t
  2. \n\n\t\t\t\t
  3. \n\t\t\t\t\t\n\t\t\t\t\t\tProducts\n\t\t\t\t\t\n\t\t\t\t
  4. \n\n\t\t\t\t\t
  5. \n\t\t\t\t\t\tFor Teams\n\t\t\t\t\t
  6. \n\t\t\t
\n\t\t\t\n\n\n\t\t \n\t\t\n\n\n\n\t
\n
\n\n\t\n\n\n\n
\n \n\n\n
\n\n\n
\n \n\n
\n
\n \n
\n\n\n
\n
\n
\n \n
Collectives\u2122 on Stack Overflow
\n

Find centralized, trusted content and collaborate around the technologies you use most.

\n \n Learn more about Collectives\n \n
\n
\n\n
\n
\n\n
\n

Teams

\n

Q&A for work

\n

Connect and share knowledge within a single location that is structured and easy to search.

\n \n Learn more about Teams\n \n
\n\n
\n \n
\n
\n\n
\n
\n \n

Get early access and see previews of new features.

\n Learn more about Labs\n
\n\n\n
\n\n\n\n
\n\n \n\n
\n \n\n
\n\n \n\n \n
\n
\n Asked\n \n
\n
\n Modified\n 1 year ago\n
\n
\n Viewed\n 88k times\n
\n
\n\n
\n \n
\n \n
\n
\n\t\t
\n
\n\n\n
\n
\n
\n \n
\n 150\n
\n \n\n\n \n\n\n\n\n\n\n\n\n\n \n \n\n
\n\n
\n\n \n\n
\n \n
\n \n

I understand Rust doesn't have a garbage collector and am wondering how memory is freed up when a binding goes out of scope.

\n

So in this example, I understand that Rust reclaims the memory allocated to a when it goes out of scope.

\n
{\n    let a = 4\n}\n
\n

The problem I am having with this, is firstly how this happens, and secondly isn't this a sort of garbage collection? How does it differ from typical garbage collection?

\n
\n\n \n\n
\n
\n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n \n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n\n
\n
\n \n
\n
\"Penny
\n
\n
\n Penny Liu\n
\n 16.2k55 gold badges8383 silver badges102102 bronze badges\n
\n
\n
\n
\n
\n
\n
\n
\n asked Sep 20, 2015 at 8:37\n
\n \n
\n
\n
\"rix's
\n
\n
\n rixrix\n
\n 10.4k1515 gold badges6969 silver badges9595 bronze badges\n
\n
\n
\n\n\n
\n
\n
\n \n
\n\n\n\n\n 8 \n
\n
\n
    \n\n
  • \n
    \n
    \n 19\n
    \n
    \n
    \n
    \n \n "Deterministic object lifetimes". Similar as C++.\n \n
    \n– user2864740\n
    \n Sep 20, 2015 at 8:41\n \n \n \n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n @user2864740 That guide is well out of date. The modern replacement would probably be doc.rust-lang.org/book/references-and-borrowing.html.\n \n
    \n– Veedrac\n
    \n Sep 20, 2015 at 21:12\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n Rust is garbage collected, like any other practical programming language. It's just that everybody thinks about garbage collection the wrong way.\n \n
    \n– IInspectable\n
    \n Feb 6, 2022 at 8:16\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n @IInspectable A definition in which any language that handles deallocation is GCed is useless because that encompasses almost all programming languages except ASM (even C). What's useful is a definition most developers already understand and go by, so no, Rust doesn't have an external reference counter which halts the execution of the program to check for unused references are free that memory.\n \n
    \n– Tin Svagelj\n
    \n Oct 19, 2023 at 21:32\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n @TinSvagelj I find the requirements that a GC necessarily must be "external" and "halt the execution of a program" arbitrary, particularly as you move on to explain that it was to do so "to check for unused references [and] free that memory". The latter is precisely what Rc<T> and Arc<T> (or Box<T> for that matter) do. Neither is external nor halts execution because they don't have to. The observable results would be identical if they were, and I wonder whether you propose that a GC only be called a "GC" if it is strictly more costly than it needs to be.\n \n
    \n– IInspectable\n
    \n Oct 20, 2023 at 11:03\n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n\n
\n\n\n
\n
\n\t\t
\n
\n\n \n \n
\n \n
\n
\n
\n

\n 4 Answers\n 4\n

\n
\n
\n \n\n
\n
\n \n \n Reset to default\n \n
\n
\n \n
\n
\n\n\n
\n
\n \n
\n\n\n \n\n
\n
\n
\n
\n \n
\n 117\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n \n
\n

Garbage collection is typically used periodically or on demand, like if the heap is close to full or above some threshold. It then looks for unused variables and frees their memory, depending on the algorithm.

\n\n

Rust would know when the variable gets out of scope or its lifetime ends at compile time and thus insert the corresponding LLVM/assembly instructions to free the memory.

\n\n

Rust also allows some kind of garbage collection, like atomic reference counting though.

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n \n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n
\n
\n \n
\n \n
\n
\n \n
\n \n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Sep 20, 2015 at 8:59\n
\n \n
\n
\n
\"Ayonix's
\n
\n
\n AyonixAyonix\n
\n 1,90211 gold badge1515 silver badges1414 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n \n
\n\n\n\n\n 8 \n
\n
\n
    \n\n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n By allocating memory when introducing variables and freeing memory when the memory is no longer needed? I don't really know what you want to say with that. Maybe we have different opinions on what a GC is then.\n \n
    \n– Ayonix\n
    \n Sep 20, 2015 at 9:06\n \n \n \n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n \n His question is how Rust's approach differs from a typical GC. So I explained what a GC is and how Rust does it without a GC.\n \n
    \n– Ayonix\n
    \n Sep 20, 2015 at 9:08\n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n \n doc.rust-lang.org/book/the-stack-and-the-heap.html explains it pretty well. Yes, many things are in the stack but let alone is no sufficient indicator (see Box). I left that out for the sake of simplicity, since the question was asking generally though\n \n
    \n– Ayonix\n
    \n Sep 21, 2015 at 21:04\n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n \n @Amomum Actually Rust doesn't have any anointed new() function like C, they are just static functions, and in particular something like let x = MyStruct::new() creates its object on the stack. The real indicator of heap allocation is Box::new() (or any of the structures that depend on Box).\n \n
    \n– Mario Carneiro\n
    \n Jul 13, 2016 at 9:34\n \n \n \n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n \n What other languages handle memory management in a similar way to Rust?\n \n \n Mar 24, 2017 at 19:03\n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n
\n
\n\t\t
\n
\n \n\n
\n
\n
\n
\n \n
\n 64\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n \n
\n

The basic idea of managing resources (including memory) in a program, whatever the strategy, is that the resources tied to unreachable \"objects\" can be reclaimed. Beyond memory, those resources can be mutex locks, file handles, sockets, database connections...

\n\n

Languages with a garbage collector periodically scan the memory (one way or another) to find unused objects, release the resources associated with them, and finally release the memory used by those objects.

\n\n

Rust does not have a GC, how does it manage?

\n\n

Rust has ownership. Using an affine type system, it tracks which variable is still holding onto an object and, when such a variable goes out of scope, calls its destructor. You can see the affine type system in effect pretty easily:

\n\n
fn main() {\n    let s: String = \"Hello, World!\".into();\n    let t = s;\n    println!(\"{}\", s);\n}\n
\n\n

Yields:

\n\n
<anon>:4:24: 4:25 error: use of moved value: `s` [E0382]\n<anon>:4         println!(\"{}\", s);\n\n<anon>:3:13: 3:14 note: `s` moved here because it has type `collections::string::String`, which is moved by default\n<anon>:3         let t = s;\n                     ^\n
\n\n

which perfectly illustrates that at any point in time, at the language level, the ownership is tracked.

\n\n

This ownership works recursively: if you have a Vec<String> (i.e., a dynamic array of strings), then each String is owned by the Vec which itself is owned by a variable or another object, etc... thus, when a variable goes out of scope, it recursively frees up all resources it held, even indirectly. In the case of the Vec<String> this means:

\n\n
    \n
  1. Releasing the memory buffer associated to each String
  2. \n
  3. Releasing the memory buffer associated to the Vec itself
  4. \n
\n\n

Thus, thanks to the ownership tracking, the lifetime of ALL the program objects is strictly tied to one (or several) function variables, which will ultimately go out of scope (when the block they belong to ends).

\n\n

Note: this is a bit optimistic, using reference counting (Rc or Arc) it is possible to form cycles of references and thus cause memory leaks, in which case the resources tied to the cycle might never be released.

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n \n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n
\n
\n \n
\n
\"Matthias
\n
\n
\n Matthias Braun\n
\n 33k2626 gold badges146146 silver badges173173 bronze badges\n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Sep 20, 2015 at 11:14\n
\n \n
\n
\n
\"Matthieu
\n
\n
\n Matthieu M.Matthieu M.\n
\n 292k4949 gold badges463463 silver badges739739 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n \n
\n\n\n\n\n 10 \n
\n
\n
    \n\n
  • \n
    \n
    \n 3\n
    \n
    \n
    \n
    \n \n "Languages with a Garbage Collector periodically scan the memory (one way or another)". Many do but that is not true in general. Real-time garbage collectors scan incrementally rather than periodically. Reference counting languages like Mathematica don't scan at all.\n \n
    \n– J D\n
    \n Jun 4, 2016 at 22:50\n
    \n
    \n
  • \n
  • \n
    \n
    \n 6\n
    \n
    \n
    \n
    \n \n "I do not count reference-counting as a complete Garbage Collection mechanism since it must be supplemented to avoid leaking cycles". RC is conventionally regarded as a form of GC. In Mathematica and Erlang, for example, cycles cannot be created by design so RC does not leak. For a high-level perspective, see "A unified theory of garbage collection" cs.virginia.edu/~cs415/reading/bacon-garbage.pdf\n \n
    \n– J D\n
    \n Jun 5, 2016 at 16:24\n
    \n
    \n
  • \n
  • \n
    \n
    \n 3\n
    \n
    \n
    \n
    \n \n "I fail to see how periodic does not cover the incremental case". Stop the world algorithms would be regarded as periodic whereas tricolor marking is regarded as incremental, for example. They are opposites in this context.\n \n
    \n– J D\n
    \n Jun 7, 2016 at 20:14\n
    \n
    \n
  • \n
  • \n
    \n
    \n 2\n
    \n
    \n
    \n
    \n \n @JD You're going way too deep. His explanation doesn't have to do with the internals of how GCs work, only the differences between GC and non-GC languages. The differentiation that you're trying to make is based on the implementation of GCs themselves. The differentiation that he's trying to make is between GCs in the abstract. There's no need to delve 500 words into the semantic meaning of "periodic" in this context.\n \n \n Sep 16, 2020 at 21:29\n \n \n \n
    \n
    \n
  • \n
  • \n
    \n
    \n 1\n
    \n
    \n
    \n
    \n \n Abstractly, we normally consider langauges like C++/Rust that use RAII/RC as non-garbage-collecting. And languages such as Java/Python/C# to be garbage collecting (Even if it uses RC as an underlying implementation). The core difference is that in C++/Rust, the RC is explicit, and it's virtually a 5-line wrapper around calling malloc and free yourself. When in a GC language, it's abstracted from view, and classes are passed by reference rather than by value. (And the language specification rarely mentioned whether or not its RC or Mark-and-sweep, that's normally an implementation detail)\n \n \n Sep 16, 2020 at 21:35\n \n \n \n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n \n\n
\n
\n
\n
\n \n
\n 7\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n \n
\n

With a language where you must manually manage memory, the distinction between the stack and the heap becomes critical. Every time you call a function, enough space is allocated on the stack for all variables contained within the scope of that function. When the function returns, the stack frame associated with that function is \"popped\" off the stack, and the memory is freed for future use.

\n\n

From a practical standpoint, this inadvertent memory cleaning is used as a means of automatic memory storage that will be cleared at the end of the function's scope.

\n\n

There is more information available here:\nhttps://doc.rust-lang.org/book/the-stack-and-the-heap.html

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n \n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Sep 20, 2015 at 8:42\n
\n \n
\n
\n
\"Swiss's
\n
\n
\n SwissSwiss\n
\n 5,69111 gold badge2929 silver badges4242 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n \n
\n\n\n\n\n 7 \n
\n
\n
    \n\n
  • \n
    \n
    \n 5\n
    \n
    \n
    \n
    \n \n While using the stack is handy, deterministic object lifetimes can still be handled if all values were 'created on the heap'. Thus it is an implementation detail; not necessarily a language strategy.\n \n
    \n– user2864740\n
    \n Sep 20, 2015 at 8:47\n \n \n \n
    \n
    \n
  • \n
  • \n
    \n
    \n 2\n
    \n
    \n
    \n
    \n \n You keep using that word. I do not think it means what you think it means.\n \n
    \n– Swiss\n
    \n Sep 20, 2015 at 8:51\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n Means what I wish to express; being the opposite of non-deterministic lifetimes. Make an offer for a better phrase.\n \n
    \n– user2864740\n
    \n Sep 20, 2015 at 8:55\n \n \n \n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n Thanks for the answer, i've give the points to the first one simply because it was submitted first. The information is just as useful and valid.\n \n
    \n– rix\n
    \n Sep 20, 2015 at 9:07\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n @user2864740 Deterministic object lifetimes refers to being able to tell exactly when the object's memory will be cleared once its destructor has been called. It has nothing to do with how that destructor is called in the first place. You keep bringing up the same term repeatedly even though it has no direct significance to the question.\n \n
    \n– Swiss\n
    \n Sep 20, 2015 at 9:09\n \n \n \n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n
\n
\n\t\t
\n
\n \n\n
\n
\n
\n
\n \n
\n 4\n
\n \n\n\n \n\n\n\n\n\n\n\n\n
\n
\n \n
\n
\n\n \n \n\n
\n\n
\n\n \n\n
\n \n
\n

Some languages have reference counting, some have garbage collectors.\nRust avoids both, instead, it allows only a single variable name or alias if you like to own a memory location at any point in time. You can move the\nownership from one variable name to another, but you can\u2019t have two variable names pointing to the same memory address (Except for shared Ownership. Rust provides the reference-counted pointer types Rc and Arc. ex: a doubly linked list).

\n
\n

Rust uses a relatively unique memory management approach that\nincorporates the idea of memory \u201cownership\u201d. Basically, Rust keeps\ntrack of who can read and write to memory. It knows when the program\nis using memory and immediately frees the memory once it is no longer\nneeded. It enforces memory rules at compile time, making it virtually\nimpossible to have runtime memory bugs. You do not need to manually\nkeep track of memory. The compiler takes care of it.

\n
\n

Discord recently switched from Go to Rust in one of its services just because garbage collector was causing latency. They explained very well why they did this and you will learn more about the garbage collector and rust memory system:

\n

https://discord.com/blog/why-discord-is-switching-from-go-to-rust#:~:text=Discord%20is%20a%20product%20focused,and%20messages%20you%20have%20read.

\n
\n
\n
\n \n
\n \n\n\n
\n\n
\n\n
\n Share\n
\n\n\n \n\n
\n \n
\n\n\n\n\n\n\n
\n
\n
\n
\n
\n
\n \n
\n \n
\n
\n \n
\n \n
\n
\n
\n
\n\n\n
\n
\n
\n
\n answered Feb 4, 2022 at 21:03\n
\n \n
\n
\n
\"Yilmaz's
\n
\n
\n YilmazYilmaz\n
\n 42.4k1515 gold badges178178 silver badges231231 bronze badges\n
\n
\n
\n\n\n
\n
\n \n \n
\n \n
\n\n\n\n\n 3 \n
\n
\n
    \n\n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n "you can\u2019t have two variable names pointing to the same memory address" - I'm pretty sure that is exactly what Rc/Arc do.\n \n
    \n– IInspectable\n
    \n Feb 6, 2022 at 8:27\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n Rc and Arc, allow values to have multiple owners, under some Restrictions.\n \n
    \n– Yilmaz\n
    \n Apr 30, 2022 at 13:31\n
    \n
    \n
  • \n
  • \n
    \n
    \n
    \n
    \n
    \n
    \n \n I don't see how that invalidates my previous comment. Either way, your comment is in conflict with your statement: "Every value has a single owner".\n \n
    \n– IInspectable\n
    \n Apr 30, 2022 at 14:26\n
    \n
    \n
  • \n\n
\n\t
\n\n \n
\n
\n
\n\n\n \n
\n \n \n \n

\n Your Answer\n

\n \n\n \n\n\n\n
\n\n\n
\n
\n
\n
\n
\n

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

\n \n
\n
\n \n
\n
\n \n
\n
\n
\n
\n
\n\n \n\n\n\n
\n
Draft saved
\n
Draft discarded
\n
\n\n\n
\n
\n\n
\n \n \n
\n\n
\n\n\n
\n
\n
\n
\n

Sign up or log in

\n \n
\n Sign up using Google\n
\n
\n Sign up using Facebook\n
\n
\n Sign up using Email and Password\n
\n
\n \n \n \n
\n

Post as a guest

\n
\n
\n \n
\n \n
\n
\n
\n
\n
\n
\n
\n \n

Required, but never shown

\n
\n
\n
\n \n
\n
\n
\n\n
\n
\n
\n \n \n\n
\n\n
\n \n \n

\n By clicking \u201cPost Your Answer\u201d, you agree to our terms of service and acknowledge you have read our privacy policy.\n

\n
\n
\n
\n\n\n

\n
\nNot the answer you're looking for? Browse other questions tagged or ask your own question.
\n

\n
\n
\n\n \n
\n \n\n \n \n\n\n
\n
\n\t\t
\n
\n
\n
\n\t\t
\n
\n
\n\n\n \n\n\n \n \n\n\n\n
\n

\n \n Hot Network Questions\n \n

\n \n\n \n more hot questions\n \n
\n\n \n \n\n
\n\n
\n\n
lang-rust
\n\n \n\n
\n
\n\n \n \n\n \n \n\n\n \n\n \n \n \n \n \n \n\n\n \n \n \n", + "page_last_modified": "" + }, + { + "page_name": "Why does the programming language Rust avoid using garbage collection ...", + "page_url": "https://www.quora.com/Why-does-the-programming-language-Rust-avoid-using-garbage-collection-for-performance-reasons-Will-we-see-more-languages-like-this-in-the-future", + "page_snippet": "", + "page_result": "", + "page_last_modified": "" + } + ] +} \ No newline at end of file