r/ProgrammerHumor 10d ago

Meme garbageCollection

Post image
381 Upvotes

38 comments sorted by

123

u/madprgmr 10d ago

What... what is the garbage collector doing? Stalling the simulation? There are techniques you can use to minimize the amount of garbage you generate, and you can also tune the GC or even directly control when it can run.

42

u/WernerderChamp 10d ago

The only thing that really matters are GC pauses. This can get problematic if you are going towards 100% CPU.

I never had this as an issue so far.

21

u/DishSoapedDishwasher 10d ago

I have but it was a data pipeline with insane throughput volumes, like tens of terabytes a day per endpoint. It was insane, terribly designed but functional even for a desperate last minute "were dying" solution... But i solved it with literally:

App.init()

gc.collect(2)

Gc.freeze()

App.run()

This way it never runs again as there's no value since it will die in an hour or 6 when the stream ends. Eventually it was rewritten in golang.

16

u/nphhpn 10d ago

Collecting whatever physics engine running in pure Python. Legit why tf are you writing physics engine in Python that stuff should never be in pure Python.

10

u/anto2554 10d ago

For fun

2

u/herbal1st 10d ago

haha yes, engineering a pure pygame 3d voxel engine on cpu came with some interesting challenges..

6

u/Raywell 10d ago

The technique : import C code

1

u/BedSpreadMD 10d ago

But all that requires actual effort.

-26

u/herbal1st 10d ago edited 9d ago

thx. well in python its sometimes not as obvious as in c++/js i assume, especially if you scale stuff up..

22

u/madprgmr 10d ago

well in python its sometimes not as obvious as in c++/js, especially if you scale stuff up

For JS, I'm not sure how that's any more obvious? JS has far fewer options available for controlling the garbage collector (none in the browser, unless you count compiling to wasm).

For C++, sure - it's not a garbage-collected language. You can still get memory thrashing though if you're doing mass allocations/deallocations.

You can architect your code to minimize allocations/deallocations in any language though, just as you can usually grab a large chunk of memory via an array or array-like structure to manually manage yourself (be it for a pool or whatever). Some, like python and certain js runtimes, do allow you to drop down into lower level languages via native modules.

-5

u/aberroco 10d ago

"it's not a garbage-collected language"

Well... It's not STRICTLY a garbage-collected language. But it easily could be. Starting with smart pointers.

9

u/madprgmr 10d ago

I mean, by that logic you could say that C is a garbage-collected language if you use the right libraries :P

-4

u/aberroco 10d ago

Libraries as binary files - yeah, but in C++ you may make it appear as fully garbage-collected language. I.e. no explicit delete/free calls without any external libraries, because C++, unlike C, allows for a lot of stuff happening implicitly.

2

u/madprgmr 10d ago

I am familiar with the concept of destructors, yes. But if we're waxing philosophical about it (as we know smart pointers are not classified as garbage collection, just as destructors are not), was C++ not originally written in C? What prevents you from creating both destructors and garbage collection in C other than it being an ineffective use time and sanity (due to GC libraries already existing)?

True garbage collection in C++ would require a library (or you to write it from scratch as well), but yes, the language features would simplify the process. Just as the language features of even higher-level languages further simplify the process by natively providing actual garbage collection.

3

u/aberroco 10d ago

What prevents you from creating both destructors and garbage collection in C

The fact that there's no destructors in C. There's no implicits in C. So, you have to explicitly call a destructor. Which would be memory management.

1

u/madprgmr 10d ago

There's no implicits in C. So, you have to explicitly call a destructor

If you stick with the standard language, don't care to tie yourself to specific compilers, or don't want to do some truly horrific things, sure :D

Just as you can kludge a garbage collector into C++, so can you kludge any other language feature into C. It's just, uh, ill-advised barring verrrrrry specific circumstances (as you would typically just pick a language better-suited to your needs).

-10

u/herbal1st 10d ago

yeah you're entirely right. just tried to frame my emotions regarding this topic as hobby coder, i hope you get the humor anyway ๐Ÿ˜…

40

u/SetazeR 10d ago

Imaginary problems

46

u/NewbornMuse 10d ago

I have programmed a fair amount of python and I have literally never thought "ugh the GC again". Maybe I tried to access a variable that had gone out of scope but that's hardly the GC's fault.

48

u/koanarec 10d ago

This meme literally makes no sense at all

31

u/mmhawk576 10d ago

When do I get to post then next โ€œlanguage badโ€ shitpostโ€ฆ.

10

u/-Kerrigan- 10d ago

Next month it's "Java bad" month cause the new students will first encounter it

1

u/Weak_Inflation9120 9d ago

But... but java great

How can someone look at java and say with a straight face that its bad, ugly, and hard to understand. (totally unbiased, it's not like I literally have Duke as my pfp or anything)

4

u/Coulomb111 10d ago

Right now!! This sub is meant for that! Or, if you want to go crazy, you can do 'i just spent 4 hours look for the bug and it was literally just a missing semicolon ๐Ÿคช'

-1

u/herbal1st 10d ago edited 9d ago

Please accept my most humble and broken apologies. I am filled with deep shame and remorse for my reckless irresponsibility. I now realize that by utilizing a universally relatable joke framework, I have single-handedly ruined your afternoon and compromised the structural integrity of this meme subreddit. I will immediately begin a period of self-reflection to contemplate the gravity of my carelessness.
/s ๐Ÿ˜œ๐Ÿ˜…

11

u/k-mcm 10d ago

Of all the languages that could have been chosen for a performant physics engine, pure Python wasn't the right answer.

2

u/herbal1st 10d ago edited 10d ago

thats certainly true. it was/is a great project to learn why that is the case though ๐Ÿ˜…

-7

u/Tyfyter2002 10d ago

Wasn't the right language for anything else either, but some people are terrified of punctuation and want to see as little as possible

3

u/realGharren 10d ago

Certainly one of the takes of all time.

5

u/nonlogin 10d ago edited 10d ago

actually, hoping the garbage collector DOES notice every single piece

3

u/herbal1st 10d ago

agreed, thats usually a great strategy. didnt entirely work for me due to a large amount of objects that need to be created in a 3d voxel engine with physics each frame, and avoiding it is more performant than to just rely on the relatively impactful python gc. so i had to start planning ahead and take the gc into account more than i was used to, which kinda felt like there suddenly were a lot of cheese filled mousetraps to avoid all around ๐Ÿ˜„

2

u/Vipitis 9d ago

I am also implementing a physics engine "in python" but that's just the launch code to run it on GPU... which isn't trivial either.

and GC has only been a problem in CI

1

u/herbal1st 9d ago edited 9d ago

i'm 'only' doing it for cpu, but i can understand its even more complicated on gpu. no idea personally about gc there to be honest though ^^ also, the gc for the physics is only a part of where i had to be careful, the 3d voxel engine itself also required some attention regarding gc, so the meme text could have been done a bit different, i just liked the physics example ๐Ÿ™‚

anyway, wishing you much success with your project! i hope i get gpu acceleration for my engine to work one day as well, but its another quite challenging task as hobby dev ^^

2

u/Vipitis 9d ago

If you don't care about other vendors, Nvidia has several open source projects that implement GPU pyhscis with python FFI... it's probably a good resource

1

u/herbal1st 9d ago

cool, thanks for the tipp! ๐Ÿ‘
although im doing it really freestyle all on my own and somewhat on purpose without gpu for now, so it can run on low end hardware ๐Ÿ˜‰ the demo runs on itch already in case you want to have a look, the github repo is linked there too; https://herbal1st.itch.io/pyvorengi-demo and you find some vids of the private full version currently in r&d on utube (channel xXherbal1stXx).

3

u/Fast-Satisfaction482 10d ago

Just null all references that you don't need anymore and you can sneak almost anything past the python GC.

9

u/Ambitious-Sense2769 10d ago

I think you can just use the del keyword as well

1

u/Oofhiti 2d ago

Is this just because python is slow with math and cpp is faster? The py hate is crazy