Moving from Javascript to something lower. Memory management?

3    18 Jun 2015 23:48 by u/slpmn

All this Webassembly talk is pretty interesting and I want to start picking up another language.

I'm currently a web developer and so my world largely consists of Javascript with some occasional Python for fun. Lately I've been flirting with the idea of learning a lower level language like C++ or Rust or something.

This idea of memory management is a little more foreign to me, and my understanding is that some of these languages are more sensitive to memory allocation, and I would just like some help getting it to click in my head.

I know generally the DOM, for example, is loaded to memory at once so, while I do keep efficiency in mind, memory management is not always on my mind.

I've done some Googling, but I'd be interested in hearing, especially from those who started with JS and went deeper, on how I should start thinking about things like this. Articles, analogies, insights - anything would help. For exanple pointers to memory addresses. Is there an analogus web dev concept (that is not literally memory addresses)? Thanks.

4 comments

0

It depends on what language do you want to learn, and what do you want to mess with. A few (well, more than a few) languages (like Nim, that compiles down to C) have dynamic memory allocation and are garbage collected, so you don't have to manage that memory yourself.

In any case it depends on what you want to do - keep in mind that I usually don't go that low. But basically memory management is allocating (malloc on C) an area of memory and freeing it after you have finished with it. Don't use that area before allocation, don't stray outside it, don't touch it after freeing it and you should be good. It will be mostly the OS that takes care of, say, finding a free space in the heap and defragmenting it.

As for pointers... I dunno? They are pointers. Think of it like this. You have this big billboard, and you want to affix something. But it is very big, and you don't know where you can put your poster so that you don't cover anything useful, so you ask the person near the board where you should put it - the board would be the ram, and you are making a malloc call.
Then the person looks around a bit and points to an area that is partly covered with old posters. "There". That's your memory pointer (even if in this case the pointer is 2D and together with the size if forms a rectangle, but I digress). If you put your poster there it's all good, but if you put it somewhere else you might cover something important, or your poster could be covered. Once you have finished you can just leave your poster up, but you need to tell the person that you don't need that area anymore - you need to call free.

0

I think if you are looking to get "close to the metal" C is a really good option. It also connects well with Python.

0

You should learn C. C++ is a massively complex language, and the most recent revisions (e.g. C++11) avoid manual memory management in favour of automatic mechanisms such as shared_ptr. And trust me when I say that if you don't understand how to do manual memory management, Rust's system probably isn't going to make a lot of sense to you. Learn C, then look at learning C++ if you want to take on something more complex.

Is there an analogus web dev concept (that is not literally memory addresses)? Thanks.

The following analogy will probably suck cause I'm not a webdev, but here goes nothing...

Think about how you can look-up different elements by their id tags. You can call getElementById() multiple times, and it'll always return the same object for the same id. That's basically the same idea as with pointers, except instead of a string you have a number, and instead of getElementById() you have an operator, *. And instead of using .id to get the id of an element, you use the & operator.

0

I highly recommend learning Rust. It is the most promising C/C++ replacement out there, and it will be a big part of the industry soon.

You should still learn C for the basic understanding. I would also recommend learning assembly, too. I highly recommend this book to do that: Programming from the Ground Up