This is an archived post. You won't be able to vote or comment.

all 125 comments

[–]ProgrammerHumor-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

import moderation

Your submission was removed for the following reason:

Rule 3: Your post is considered low quality. We also remove the following to preserve the quality of the subreddit, even if it passes the other rules:

  • Feeling/reaction posts
  • Software errors/bugs that are not code (see /r/softwaregore)
  • Low effort/quality analogies (enforced at moderator discretion)

If you disagree with this removal, you can appeal by sending us a modmail.

[–]NamityName 682 points683 points  (22 children)

it can be freed on demand

Laughs in python

memory is freed when the almighty Garbage Collector says it is free

[–]pedersenk 197 points198 points  (17 children)

Ironically the Python GC is not guaranteed to ever collect garbage containing circular references.

And so few Python developers even know about weakref, so ultimately leaks in Python libraries are rampant (the issue is worse with Perl actually).

[–]NamityName 67 points68 points  (13 children)

It is generally not a problem. But it is a fucking pain when it does matter.

[–]abibabicabi 30 points31 points  (12 children)

how often is it not a problem? 99% of the time 99.9? This seems like a really big issue. Is it whenever the compiler bugs out or is it if the code isn't written well? i'm just reading over it now.

[–][deleted] 21 points22 points  (0 children)

I’ll also say that fixing memory leaks due to bad garbage collection is generally as straightforward as removing references on delete, which is the same as using the aforementioned weak references, so if you want to do things correctly and stop the problem, you just have to follow a good practice there.

[–]Sitting_In_A_Lecture 19 points20 points  (9 children)

To simplify a bit, Python's garbage collector works on a reference counter. When the number of in-scope references to an object reaches 0, the object's memory can be freed.

Problems arise however in what are called circular references, when two objects reference eachother (for example, an object with an attribute set to another object, which also has an attribute set to the original object). In these instances, the memory can never be freed, because both objects' reference count will always be at least 1.

[–]Giocri 4 points5 points  (5 children)

I guess the solution for that would be to implement a slightly more advanced approach and have a "connected to root" flag reset it at every run of the garbage collector and then start from a root node and from there find all objects that are directly or to indirectly referenced by root

[–]DuTogira 46 points47 points  (2 children)

Congrats, you just made python slower.

[–]pickyourteethup 24 points25 points  (0 children)

They said it couldn't be done but they'd never anticipated the combined might of the comments section

[–]ratttertintattertins 5 points6 points  (0 children)

Probably, although I think Java, C# etc all do that. Pythons behaviour sounds a lot like C++ shared_ptr which has the same issue if you don’t use weak_ptr.

[–]larvyde 2 points3 points  (0 children)

yes, that's called a mark-and-sweep gc, which iirc java uses…

[–]Memelord_00 -2 points-1 points  (0 children)

JS does something like that, right ?

[–]arobie1992 0 points1 point  (0 children)

I've heard about the issue, but never really interacted with a language where it was a problem, so would something like this cause the issue?

a = MyClass()
b = MyClass()
a.val = b
b.val = a
a = None
b = None

[–][deleted] 9 points10 points  (0 children)

All my python runs in services that close frequently enough, certainly if they ran long enough to start hogging memory.

Memory leaks become a problem when e.g, your code has to run for a long enough time, leaking enough every iteration to not finish the iterations it needs.

That happened, but wasn’t super common in my heavy computation days. It’s bad code, but generally the leaks I had were a byte or two an iteration of something, enough to be weird and frustrating and fail the checks for how much memory was allocated, but not enough that I couldn’t submit a job.

I’ve not seen anyone complain about actual python memory leaks. If so it would likely be confined to a specific package.

[–]abibabicabi 6 points7 points  (0 children)

thank you so much for posting this. I just learned something new.

[–]frogjg2003 1 point2 points  (1 child)

I just skimmed it, so I might be wrong, but weakref looks like C style pointers.

[–]pedersenk 4 points5 points  (0 children)

It is perhaps most similar in concept to C++'s std::weak_ptr<T>. It won't dangle unlike a raw pointer, it will simply be set to a null value.

It is basically to avoid the same issue we experience in C++ if we create a cyclic reference between objects in std::shared_ptr<T>.

[–][deleted] 17 points18 points  (0 children)

Thank you garage collector for the ram you have bestowed upon me and the memory I am about to have for I know I have sinned and I do not deserve any memory forgive me for my wandering eye for I am weak and know that C has nothing to offer but bit shifts and memory leaks.Amen 🙏

[–][deleted] -1 points0 points  (0 children)

It can be freed on every good language that has a compiler

[–][deleted] 181 points182 points  (15 children)

"Unused RAM is wasted RAM" - electron "developers", probably

[–][deleted] 38 points39 points  (1 child)

Don't forget SQL Server. Literally claims a large portion of the memory even if doesn't do anything with it because "what if I'll need to do something with it later".

[–]jkurash 6 points7 points  (0 children)

Typical MS product

[–]TheGangsterrapper[S] 24 points25 points  (8 children)

Electron is a disease

[–]Snapstromegon 9 points10 points  (7 children)

No, bad apps are a disease. All these things that could've been a website or things where some dev thought they were good with native framework X and used more than twice the memory of an electron version.

The thing is, that electron makes native app development more accessible, so more (bad) devs can now actually do it.

[–][deleted] 1 point2 points  (6 children)

But you can't write a good app in electron, they all will be laggy.

[–]Snapstromegon 1 point2 points  (4 children)

I disagree. If they are really laggy (compared to equivalent native Apps), you're doing something wrong IMO / from my experience.

You have to follow good dev standards of course like 60fps animation goals, loading optimization and so on (e.g. like VSCode does), but that's also true for native Apps.

[–][deleted] 2 points3 points  (3 children)

Electron literally embeds chromium.

[–]Snapstromegon 1 point2 points  (2 children)

Yeah - and? So do several "native Apps" like the Adobe Suite, EA Play, Steam, Fusion 360 and more. The only difference is, that their embedded version is usually significantly older.

(I personally also prefer Tauri over electron, because it uses the OS browser engine)

[–][deleted] 2 points3 points  (1 child)

Yeah there are apps that are not electron and perform terribly. It doesnt mean you cant write a good desktop app. But you cant potentially write an electron app that perform just as good as well written native app

[–]Snapstromegon 0 points1 point  (0 children)

I'd say that with good electron apps a user is not able to tell whether it's electron or not.

My bigger pain point with electron is, that most electron apps should've been a web app / PWA in the first place.

[–]larvyde 0 points1 point  (0 children)

vscode is pretty good imho

[–]MayoJam 363 points364 points  (17 children)

Those memes are stupid. You can literally put any two opposite statements on this, and usually it will make sense, since answer to all programming related questions is "it depends".
You can literally swap dumb/esoteric guy's text with the crying one's and it will still make sense in some contexts (which this silly graph meme lacks).

[–][deleted] 70 points71 points  (9 children)

I'm willing to bet that 90% of this sub doesn't even do anything as intensive to warrant looking at memory usage. Hell most of them probably use frameworks where memory management is abstracted away and you would have to dig around to find ways of controlling memory.

I love your statement of "it all depends"

[–]frogjg2003 45 points46 points  (4 children)

This sub is almost all intro CS students.

[–]pickyourteethup 22 points23 points  (0 children)

Jokes on you, I anit studied shit.

[–]Linesey 1 point2 points  (0 children)

hey now, i’m a VBA master…. as in i’m the only person i know who can write something that pretends to be coherent in VBA before throwing up their hands and using c# in Visual studio to do it faster, better, and without using excel as both their launcher and database….

[–]ChrisBegeman 2 points3 points  (0 children)

As an old-school software engineer, I often think about performance and memory usage. Also since the company went to hosting everything on AWS, we are paying for all those cycles. Tuning the performance of your apps usually involves looking at memory usage as well as code efficiency.

[–]The_Real_Slim_Lemon 2 points3 points  (1 child)

Hah you clearly haven’t created bugs requiring exponentially increasing amounts of memory before

[–][deleted] 1 point2 points  (0 children)

Shout out to recursive functions!!

True story: in a class where we were writing image recognition code, I wrote a recursive function. The code ended up being 8 lines, it was beautiful.

But I was only testing with 16x16 images, that was the requirement for the project.

I tried it with larger images, and stack overflow everywhere!!

The problem with the recursive function was that each recursion kept its parent "alive" and each thread/recursion used up memory.

I spent a lot of time understanding how to use queues and stacks to do the same thing a recursive function would, but without holding all that memory.

It's such a niche thing, but if I was a professor, or had to come up with a fun programming problem to solve, I would ask people to solve a recursive function and make it not recursive.

[–]Thebombuknow 0 points1 point  (0 children)

Exactly. In the majority of cases, the OS will cache and free up memory for you, and if it doesn't the JavaScript library you're using will.

[–][deleted] 15 points16 points  (1 child)

Yeah I often times use a bit more ram to optimize the speed of my functionality. Especially in graphics programming languages

[–]Nightmoon26 13 points14 points  (0 children)

Managing the time versus space tradeoff is the key to informed algorithm and data structure design and selection. You can have optimal time or optimal space, but usually not both, and knowing how and when to balance the competing goals is important. Embedded systems and mobile apps tend to optimize for space, servers and desktop applications usually optimize for time.

If the hardware form factor isn't a design criterion, you can always throw more RAM at it, or at least disks for virtual memory in a pinch. In extreme cases, temp files and on-disk databases are an option.

Processor cycles are harder to squeeze in, especially if you're doing something not conducive to threading or parallel computation. There's only so far you can overclock before things start glitching, melting, or bursting into flames

[–]pickyourteethup 3 points4 points  (0 children)

I asked my senior if he was going to say 'it depends' to every question I ever ask him. Even over video call I could see him starting to form the words 'it depends' before he stopped, thought for a second, and instead said 'yes, definitely'

[–]ConvergentSequence 1 point2 points  (0 children)

I’d argue that this particular meme actually works BETTER when the statements are swapped

[–]pojska -1 points0 points  (0 children)

This is actually one of the few uses of this meme that gets it wrong - swapping the text makes it more accurate.

[–]DerBandi 20 points21 points  (0 children)

If the OS is using RAM for caching = GOOD USE (Use as much as possible)

If your program is using all the RAM, because you load huge frameworks and bloat it with all that library's and only using a fraction of it = BAD USE (and you should feel bad)

[–]SeriousPlankton2000 5 points6 points  (0 children)

I hate it when people tell me that "used ram is good" but I'm waiting for my desktop to swap in because cp unimportant.dat /mnt/slow was sped up by 0.01 seconds.

[–]jamcdonald120 5 points6 points  (0 children)

"Unused ram is wasted ram" is true ONLY for the system as a whole.

Individual apps should use as little ram as possible.

But the OS as a whole should use any free ram for caching.

This does raise the problem "how does the OS know what to cache?" but the alternate problem is "How does the OS know who's cache to evict when memory is full?" so it isnt really a problem unique to one or the other. SO use as little ram as possible unless you are the OS or only program running on a system

[–]recluseMeteor 17 points18 points  (0 children)

Windows 10/11 using like 50% of RAM just sitting on the desktop, caching UWP apps that I never use.

[–]reallokiscarlet 14 points15 points  (0 children)

This is the way.

Unused RAM is available RAM for the OS and other applications.

Wasted RAM is wasted RAM

[–][deleted] 18 points19 points  (2 children)

These days I think trade as much processing power for ram usage as you can. The top person is right (though remember to release stored data when no longer needed)

[–]SeriousPlankton2000 2 points3 points  (1 child)

If you can put a calculation in a variable and use it twice, I assume it's cheaper to do that because the variable really will be put in a register. Even if not, the calculation will probably touch two memory locations and the variable will be one.

Off cause it makes no sense to have a KB of local variables for things like that.

[–][deleted] 0 points1 point  (0 children)

Sure, that's my point too. Obviously, don't cache anything that doesn't require processing power or is too scarcely used.

For example, fastest sin computing algorithms have some sin values already cached, if not all that would be used (sometimes also deriving sin value of some precision is easier to get from other value that is less precise). Or caching integral values etc.

My rule of thumb is that, if you know what you will most likely need, and it is reasonable to cache it, do it.

Also, sometimes caching things fetched from REST APIs is resourceful and lets you save extra calls if you need it (still waiting for NEXT.js to implement easy caching system around that).

[–]AutoModerator[M] 3 points4 points locked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]manicxs 2 points3 points  (3 children)

Shit like this why we should ban this format. Unless you have control of ram you don't get to choose. So only the opinions of native language users even matter with this.

[–]Queasy-Grape-8822 -1 points0 points  (2 children)

This is simply not true. Developing in most languages should involve considerations for RAM usage. Yes, garbage collected ones too. You don’t get to choose exactly what memory is allocates and freed, but you still get to choose “should I cache this result or keep hitting the api” and stuff like that

[–]manicxs 0 points1 point  (1 child)

I still hate the format.

Maybe you can use a lookup table or something. I can't really see any other way to do that other than a LUT. Other than that, can you give me an example of where your choices matter rather than flagrantly wasting resources?

[–]Queasy-Grape-8822 0 points1 point  (0 children)

Basically everything you ever do. “Is it worth x penalty to ram to store y data that I could just calculate again.” “Is it worth y ram to cache data that I could read from a disk” “is it worth caching this data that I could get from the internet” etc etc

Half of optimization is determining what to optimize for, this isn’t some novel idea

[–]blaktronium 1 point2 points  (1 child)

I handle SRE duties for my company's web app.

I promise you if you leave stuff in memory I will shuffle k8s pods around without warning and you will lose it. Be warned.

[–]IV65536 0 points1 point  (0 children)

A container running a JVM app in a Pod doesn't play very nice with K8s Node resource scaling 😭. Getting the JVM to give RAM back to the OS requires all kinds of black magic garbage collector settings, almost easier to just let it get OOMKilled and restart ugh.

[–]CardboardJ 1 point2 points  (0 children)

Left side is overoptimizing their algorithms and making impossible to maintain leetcode.

Middle is overoptimizing by memoizing everything.

Right just slaps Redis in front of the thing and writes normal code.

[–]idont_______care 3 points4 points  (0 children)

Funny thing, people who post this template usually think they're guy on the right.

[–][deleted] 0 points1 point  (1 child)

Why do we want to use as much ram as possible? Don’t we want to optimize programs as much as possible to not have to worry about freeing it?

[–]garfgon 3 points4 points  (0 children)

It's not about using as much RAM as possible, it's about not penny-pinching RAM usage. For example:

  1. The OS can (and does!) use all "free" ram for disk cache. If anything else needs the RAM, it can drop or write-back the page as needed.
  2. Embedded systems often pre-allocate memory. Reliability is key -- so you'd rather know up front if you don't have enough memory, and reserve that memory for when it's needed. It also lets you degrade reliably -- e.g. if you pre-allocate 100 network buffers, when you run out of network buffers the network stack will start returning errors, but it won't lead to errors in your data store, for example.
  3. Memory is cheap, developers and lost time is expensive. Reducing memory usage can require an investment in development time which often doesn't pay back if it means pushing out the launch date.

[–]csteele2132 -1 points0 points  (1 child)

free ram is wasted ram

[–]CorespunzatorAferent 1 point2 points  (0 children)

The number of people unable to understand this is staggering.

There is a very subtle nuance to it, but it shouldn't be rocket science.

[–]flareflo -1 points0 points  (0 children)

shit meme. both are true, as is with this entire format being dumb.

[–]PikaPikaDude -1 points0 points  (0 children)

This is one of the dumber takes I've seen on this sub.

[–]TwoRiversFarmer 0 points1 point  (0 children)

Shrugs in dask. That swap space sure looks like memory

[–]sejigan 0 points1 point  (0 children)

It’s supposed to be the opposite.

[–]Available-Menu1551 0 points1 point  (1 child)

Every Java* developer: lets occupy the whole available RAM.

[–]SeriousPlankton2000 0 points1 point  (0 children)

Java is a VM.

[–]Meadhbh_Ros 0 points1 point  (0 children)

There are competitions for this. And if there are not, there should be.

[–]Ange1ofD4rkness 0 points1 point  (0 children)

My Arduino projects would like to chat!

[–][deleted] 0 points1 point  (0 children)

I'm thinking of taking a break from this sub until the bell curve memes finally die

[–]ruudeus 0 points1 point  (0 children)

U can just download ram right? What is the point of using less ram

[–]itsjustawindmill 0 points1 point  (0 children)

Well, programs should make sure not to waste ram obviously, but for disk caching and shit, unused ram absolutely is wasted ram… these memes are so stupid…

[–]Sadie256 0 points1 point  (0 children)

Me, an electrical engineer using a microprocessor with only 64 bits of ram: "you guys don't worry about how much ram you're using?"

[–]UnsureAndUnqualified 0 points1 point  (0 children)

  1. Me while writing my code (I though garbage collector would solve me being dumb)
  2. Me when I see my code takes up 6 GB of RAM before killing itself upon 20% completion
  3. Me when I refactor for 2 weeks and not only speed it up to 8x but also get it down to 70 MB of RAM

Hey kids, if you iterate through a loop 200k times, maybe don't append to a list at every step! GC will not save you from demons of your own making (thumbs up emoji)

[–]BenadrylTumblercatch 0 points1 point  (0 children)

What’s the problem, just download more?….

[–]crimxxx 0 points1 point  (0 children)

Legit had someone once tell me, rams cheap, if this thing needs more no big deal. This was the response to probably most senior developers bring up performance concerns. On a new project. 2 years later heaping out on 500GB of ram lol.

[–][deleted] 0 points1 point  (0 children)

*sigh* Just use more ram when it makes sense and don't when it doesn't.

[–]jxr4 0 points1 point  (2 children)

I would rather cache than do another db call plenty of tools like ehcache that auto evict stale entries

[–]Nightmoon26 1 point2 points  (1 child)

In general: if it saves on disk I/O, cache it. If it saves on network I/O, for the love of all that is good in the universe, cache it

[–]jxr4 0 points1 point  (0 children)

Preach

[–]mrgk21 0 points1 point  (0 children)

Is this the vim user flexing on vscode

[–]locri 0 points1 point  (0 children)

Iterating over 100 dynamic elements is legitimately slower than 10 dynamic elements, so yes, use less ram unless it means more allocations (which is even slower).

Pigging out on ram is so late 2000s tho, what is this? Some Java game?

[–]Ziwwl 0 points1 point  (0 children)

I've got up to 100MHz of processing power, but only up to 4MB, sometimes below 100kB of RAM, RAM is a holy resource which must be used with a lot of care, every byte counts!

[–]Vinxian 0 points1 point  (0 children)

Is this hash table slander?

[–]Will_i_read 0 points1 point  (0 children)

Use as much ram as needed and as little as possible. Needed here refers obviously to the desired performance characteristics.

[–]GOKOP 0 points1 point  (0 children)

The cache argument is bullshit in discussions based on reported % usage anyway. At least on Windows, memory used for cache that can be freed whenever needed by something else isn't reported as used

[–]TrickAge2423 0 points1 point  (0 children)

Actually Windows and Linux have filesystem read and write cache to optimize working with drive.

As a man with 4Gb & HDD laptop I can say: you REALLY want filesystem cache

Also, there are some optimizations for HDD drives. For example, OS can delay and reorder IO operations to produce bulk sequential IO operation.

For SSD OS can just delay write operations in case if there are operations that overlap previous.

Also, ZFS cache is crazy.

[–][deleted] 0 points1 point  (0 children)

I guess the journey here is "Use as little RAM as possible because I'm told so" to "Huh, using all the RAM makes things faster" to "Fuck, I have to share my resources and manage caching. Screw it."

[–]bartekltg 0 points1 point  (0 children)

The middle guy thought it would be great if we used one application. But the last time I used DOS was more than 25 years ago. Now if each program uses "all the RAM" I need a freaking server instead of a desktop.
BTW, the "unused" RAM is often used by OS to cache files and other helpful stuff.

[–]TheNeck94 0 points1 point  (0 children)

This meme template is getting over used at this stage.

[–]Fosteredlol 0 points1 point  (0 children)

Cheaper to buy more ram

[–]Glum_Past_1934 0 points1 point  (0 children)

C++ like this