Live data from Hacker News

Everything old is new again: memory optimization

nibblestew.blogspot.com

161–168 of 168 posts

Re: Everything old is new again: memory optimization

#161

Been waiting for online commentary about programming to start acknowledging this situation as it pertains to writing programs Memory and storage are not "cheap" anymore. Power may also rise in cost Under these conditions, memory usage and binary size are irrefutably relevant^1 To some, this might feel like going backwards in time toward the mainframe era. Another current HN item with over 100 points, "Hold on to your…

Idk about "memory is cheap" but storage certainly is.

Whether you're talking about buying or renting in the cloud - RAM is 3 orders of magnitude more expensive

Re: Everything old is new again: memory optimization

#162

Earlier quoted context omitted.

M&S also doesn't necessitate having a moving and compacting GC. That's the thing that actually makes the JVM's heap greedy. Go also does M&S and yet uses less memory. Why? Because go isn't compacting, it's instead calling malloc and free based on the results of each GC. This means that go has slower allocation and a bigger risk of memory fragmentation, but also it keeps the go memory usage reduced compared to the JVM…

Compacting reduces memory usage - that's why it's called compacting. The JVM uses a lot of memory a) because it's tuned for servers and not for low memory usage and b) because Java is a poorly designed language without value types.

> Compacting reduces memory usage

No, it reduces memory fragmentation, which is why it's called compacting and not compression.

I do agree that the lack of value types is a big contributor to why Java uses so much memory. But it's not a server tuning thing that makes the JVM lean memory heavy.

The JVM uses moving collectors and that is the big reason why it prefers having so much memory available. Requesting and freeing memory blocks from the OS is an expensive operation which the JVM avoids by grabbing very large blocks of memory all at once. If you have a JVM with 75% old gen and 25% new gen, half that new gen will always be empty because the JVM during collection moves live data from one side of the new gen to the next. And while it does that, it slowly fills up old gen with data.

Even more modern collectors like G1 prefer a large set of empty space because it's moving portions of old gen to empty regions while it does young collection.

As I mentioned, the difference here between the JVM and python or go is that python and go do no moving. They rely heavily on the malloc implementation to handle grabbing right sized blocks from the OS and combating memory fragmentation. But, because they aren't doing any sort of moving, they can get away with having more "right sized" heaps.

Re: Everything old is new again: memory optimization

#163
post #91

Earlier quoted context omitted.

There's bound to be a way to turn a stream of bytes into a stream of unicode code points (at least I think that's what python is doing for strings). Though I'm explicitly not volunteering to write the code for it.

Sure, but making one string from the file contents is surely much better than having a separate string per word in the original data. ... Ah, but I suppose the existing code hasn't avoided that anyway. (It's also creating regex match objects, but those get disposed each time through the loop.) I don't know that there's really a way around that. Given the file is barely a KB, I rather doubt that the illustrated techni…

I dislike loading files into memory entirely, in fact I consider avoiding that one of the few interesting problems here (the other problem being the issue of counting words in a stream of bytes, without converting the whole thing to a string).

If you don't care about efficiency you can just do len(set(text.split())), but that's barely worth making a function for.

Re: Everything old is new again: memory optimization

#164

Earlier quoted context omitted.

Compacting reduces memory usage - that's why it's called compacting. The JVM uses a lot of memory a) because it's tuned for servers and not for low memory usage and b) because Java is a poorly designed language without value types.

> Compacting reduces memory usage No, it reduces memory fragmentation, which is why it's called compacting and not compression. I do agree that the lack of value types is a big contributor to why Java uses so much memory. But it's not a server tuning thing that makes the JVM lean memory heavy. The JVM uses moving collectors and that is the big reason why it prefers having so much memory available. Requesting and free…

> No, it reduces memory fragmentation, which is why it's called compacting and not compression.

…which reduces memory usage because you don't have to waste it on free holes in the allocated pages.

Re: Everything old is new again: memory optimization

#165
post #79

Earlier quoted context omitted.

There's bound to be a way to turn a stream of bytes into a stream of unicode code points (at least I think that's what python is doing for strings). Though I'm explicitly not volunteering to write the code for it.

import mmap, codecs from collections import Counter def word_count(filepath): freq = Counter() decode = codecs.getincrementaldecoder('utf-8')().decode with open(filepath, 'rb') as f, mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm: for chunk in iter(lambda: mm.read(65536), b''): freq.update(decode(chunk).split()) freq.update(decode(b'', final=True).split()) return freq

Oh that's neat, though I might split this into two functions in most cases, no need to entangle opening the file and counting the words in a filelike object.

That's two neat tricks that I'm definitely adding to my bag of python trickery.

Re: Everything old is new again: memory optimization

#166

Earlier quoted context omitted.

> Compacting reduces memory usage No, it reduces memory fragmentation, which is why it's called compacting and not compression. I do agree that the lack of value types is a big contributor to why Java uses so much memory. But it's not a server tuning thing that makes the JVM lean memory heavy. The JVM uses moving collectors and that is the big reason why it prefers having so much memory available. Requesting and free…

> No, it reduces memory fragmentation, which is why it's called compacting and not compression. …which reduces memory usage because you don't have to waste it on free holes in the allocated pages.

But then increases memory usage because you need more pages allocated to move memory to.

It's an allocation and cache optimization more than a memory saving optimization.

Re: Everything old is new again: memory optimization

#167

I'm always confused as hell how little insight we have in memory consumption. I look at memory profiles of rnomal apps and often think "what is burning that memory". Modern compression works so well, whats happening? Open your taskmaster and look through apps and you might ask yourself this. For example (lets ignore chrome, ms teams and all the other bloat) sublime consumes 200mb. I have 4 text files open. What is it…

I did memory allocation profile for the Linux kernel. Sure would be nice if we had the same capabilities in userspace.

Re: Everything old is new again: memory optimization

#168

Been waiting for online commentary about programming to start acknowledging this situation as it pertains to writing programs Memory and storage are not "cheap" anymore. Power may also rise in cost Under these conditions, memory usage and binary size are irrefutably relevant^1 To some, this might feel like going backwards in time toward the mainframe era. Another current HN item with over 100 points, "Hold on to your…

Hard drive prices increase 40-60% in months

https://storedbits.com/hard-drive-prices-increase/

It seems like the AI apocalypse has come for hard drives

https://www.ign.com/articles/the-ai-apocalypse-has-come-for-...

Expect HDD, SSD shortages as AI rewrites the rules of storage hierarchy; multiple companies announce price hikes

https://www.tomshardware.com/pc-components/storage/expect-hd...

Hard Drives Are Sold Out for 2026; AI Data Centers Are to Blame

https://www.gadgetreview.com/hard-drives-are-sold-out-for-20...

AI Data Centers Are Causing a Surge in Hard Drive Prices

https://technewsjunkies.com/fundings-and-exits/ai-data-cente...

Post reply on HN