The world of Memory

U, 64 bits tall, is a floating point variable in the number family of the memory world. Because the binary digit of little U’s body is 0, she is treated as a girl by IEEE 754. Her rank numbers 2 to 11 are not large enough to make her look small; And her remaining 52 decimal places are so delicate that when working with her, the rounding error is very small, so everyone loves her.

Xiao U’s daily job is to deal with other variables in the memory world and calculate useful results to benefit the human world. Normally, after the function call ends, u can leave work and return to her home in the source code. Her job is not as stressful as the constant working hours of variables called I and J in the for loop. And her home has been written by human programmers since she was born. Despite their scruffy clothes, programmers love the source code like their children. Xiao U’s home in the source code is built with a kind of material called JavaScript. There are not only colorful editor themes to decorate, but also rigorous semicolons and parentheses to ensure the stability and symmetry of the structure of the home, so that she is very safe.

Despite her cute appearance, relaxing job and comfortable home, Xiao U still has her own troubles: her family background prevents her from having a partner.

In the land of JavaScript, the Number family belongs to the ancient family of basic types. With the exception of number, classical data structures like string and null belong to the primitive type family. Because simple primitive types are easy to infer from code by the interpreter, their memory is pre-allocated and immutable in a rigid “stack” space. Even if you add and subtract with other numbers, you can’t really be together.

As opposed to the family of primitive types, there is the funky family of reference types. The so-called “object-oriented programming” favored by human programmers speaks to this family. The members of the family are complex and varied, so they are divided into vast “heap” Spaces, often with each other in a state of reciprocity. A family of reference types with objects is certainly more nourishing than a family of basic types doomed to a lonely life.

Small U has a dream that dare not say, that is to strive to become a member of the reference type. She might be able to stop being alone when she hears that in distant Java, there’s a law called “autoboxing” that makes her family look like a family of reference types.

In spite of her dreams, she was actually quite satisfied with her life. After getting used to the memory world, work-life balance is something many human-world programmers will never achieve in a lifetime. And so it went on until one day…

Curse of closures

That day, as usual, U started from the source code home, passed through the lexical analysis gate, and caught the grammar analysis bus track. The JIT symbol on the bus stands for just-in-time, Just like the JR Shinkansen and Harmony In the human world, it is a symbol of efficiency and speed.

The shuttle bus quickly loaded Little U onto the leaf platform on the grammar tree track. When we get off the train, there is a 64-bit bench on the platform. She sits in a chair and closes her eyes, waiting for the interpreter to scan and invoke her.

“Let’s hope we don’t run into vulgar null values this time…” As u mused, there was a flash of electricity, and the interpreter duly read u’s value as the state of the countless transistors at the bottom of the memory world changed. In this atomic instruction, U needs to give the interpreter complete control of herself, and she never knows what happens in the memory world between the moment she flashes and the moment she opens her eyes again.

“Well…” She woke up, as expected, on the same platform, waiting for the grammartree bus to pick her up.

It was the same scene, no, there seemed to be something different — the structure and arrangement of the platform seemed the same as before, only one thing was missing: the tracks were empty, there was no train waiting for her, no one else. Don’t… Was late? She did not believe that such a low-level error would appear in the memory of the precise frequency of the world. But when the train didn’t come, she had to wait on the platform.

As time passed Hertz by Hertz, u’s inner restlessness and anxiety slowly increased: what happened? Did the bus forget me? Or did you just leave early? It’s not safe for a girl to be out alone for so long, but as a rigorous variable, going alone is a big no-no in the memory world. “Better… wait…” Small U some despair to think.

The bus still hasn’t arrived.

“No, I have to go back to the source code!” The wait finally let xiao U’s emotions up, she began to look for other exit on the platform, to find the way home. You can’t jump off the tracks, but there’s a red Exit sign at either end of the platform, which looks like a passable Exit. However, variables in modern programming language countries generally never behave this way, because manual memory manipulation is dangerous.

Small U looked around, carefully opened the door under the return Exit rusted. Thankfully, there is a road and it doesn’t look so dangerous. She walked down a long, narrow hallway that lit up at regular intervals with a small indicator light that looked like a subscript to the memory address space. Finally, she saw the Exit: an Exit door of the same shape. U can’t wait to open the door to see if he is closer to home.

She was amazed at what she saw: the same tracks, the same benches, the same platform, the same Exit, as if she hadn’t moved at all!

Am I going the wrong way? It can’t be! Little U had absolute confidence in the non-zero or one direction, and she knew she couldn’t go wrong. Maybe the contents of this address space are like that? It’s okay. A little more walking will make a difference. So, naively, she began the long walk, but to her slight loss of confidence, every Exit led to the same platform, no difference, even the rust was the same. “Hello! She began to call for help, though it seemed in vain. After holding on for a while, she finally felt like giving up and sat down wearily on a platform bench resigned to her fate.

“Are you lost?”

A sound in her ear woke her up and she curled up to see where it came from. This is also a floating point number from the number family. The first one is a boy, with a higher order and a rough decimal place.

“Who are you… and where is this?

“I’m little S, and this is the closure heap space.”

“Closures… stacks?”

“Yes, our family variables are usually allocated on the stack, and the life cycle of each call can end very quickly. But we can’t be wiped out yet because we don’t know which function still refers to us…”

“Wait! What is a life cycle? Is my life going to end?”

Seeing the confused appearance of little U, little S was surprised: The first death occurs when we leave the scope, such as after a function returns. At this point, we are not found in the context. Our second life cycle ends, but is not destroyed immediately. Second, when there are no more references to us in memory and the interpreter does garbage collection, that’s when we leave memory for good and go back to the source code, and third, when the human programmer removes our definition code, and that’s when we die.”

“So… does it mean that every time I go back to the source house…”

“Yes, the first two deaths will occur. But as long as the source code is not deleted, we are still in the world. And the first two deaths happen so quickly that we don’t even feel them.”

“But, so to return to the source of me or me?”

“Don’t ask me such profound questions… but if you put it that way, a man can’t step into a river twice!”

“Oh… it seems so… but what heap did you say…” Little U still looks confused.

“Oh, come on! We’re basic types, but we’re not necessarily on the stack. We could be used dynamically by reference types, and we could be assigned to the heap.” Little S is still preaching in a serious way.

Closure… Reference type… The heap… Small U suddenly realized that his original space, has not been able to release her in time to return to the shuttle bus stack space. Since a function or reference type has several points to it at the moment, she is allocated dynamic heap space — which is what she always wanted! However, since the interpreter’s automatic memory reclamation of heap space is not yet running, she now has to roam the space with little S, as if cursed.

“So, can we go back together?

Circulating leakage

“I’m sure we could have gone back together, but it’s weird because the interpreter is supposed to reclaim all memory in our area, and nothing’s happened yet…” Although s seems knowledgeable, she is still a little confused about the situation.

“Could someone else be using it in this area…” Little U’s judgment seems to have returned.

“If the normal memory allocation had been followed, it would have been automatically reclaimed by now. Except for a memory leak… ahh!” Little S seems to be scared of herself.

“What’s that?”

“It’s a long story… let’s just say that some of the older countries in the memory world let the programmers in the human world release us manually. This rule often leaves out some variables and causes us a lot of pain.

“Eh? Isn’t that nice?”

“Oops, the auto-recycle code was written by some stupid programmer, so there will be problems. Like that crappy Internet Explorer browser that has a problem with looping references… oh yeah!

“Loop… quote…?”

“The simplest way to think about it is this: if we are not floating-point objects and are referential objects, then as long as u has a property that points to me and I have a property that points to U, it is a circular reference.”

The face of small U suddenly red. But dull little s or gushes, “modern browsers do memory recovery algorithm is common tag removal algorithm, this algorithm has no problem of circular reference. But the early Internet explorer with a call reference counting algorithm, this algorithm in that case the reference count is not reset, this memory will not be the interpreter collected……”

“Ah… so we can’t go back?”

Reconstruction of rebirth

Small U doubt small S pulled out from the ocean of knowledge. Now they understand the situation: the two solitary primitives cannot be automatically reclaimed, and as long as the user does not shut down, they are stuck here forever, like in Inception. And it has been mathematically proven that the downtime problem is unsolvable. A long silence fell between them.

Finally, small S broke the silence: “actually… I thought of a method, can try.”

“Well, what is it?”

“The snippet of code I’m in should still execute, and at that point I’ll try to raise an exception to make the program die.”

“But we’re all here. How can something that’s already correct report an error?”

“You don’t know how weird JavaScript is. Brendan Eich, the founding father of the United States, reportedly wrote the basic state policy in one weekend, so the language is full of holes, and even the seemingly well-formed code is often messed up by human programmers.”

“So, how…”

“For example, even though I’m floating point, because I declare it in if, I can use a design flaw called variable promotion to temporarily make myself undefined if I want to.”

“In that case, the type is wrong.”

Small s confidence again: “yes, as long as I seized the opportunity to put this time I do an operation, and other variables, can make the return type from floating-point number into a dangerous NaN. Such use behind the result must be wrong, even if the program does not collapse, the human world of the user or programmer can also find the problem.”

“What can they do when they find out?”

“Will refactor my code, and you can go back.”

“That way, if your code is gone, isn’t it…”

“Yeah, nice to meet you…” Little S was already edging to the edge of the platform, where an open curly brace was blocking him. He looked at the tile in front of the braces and stamped on it. All of a sudden, the variable lift takes him out of scope. Not many Hertz later, the platform began to shake beneath the ground, and there was the sound of burning error objects throwing call stacks from the layers below. With a loud bang, the error object tore the ground — the last thing U remembered.

The next shot in memory, she was on the grammar tree bus back home. Go back to the source code, then wait for the call, and everything seems natural again, as if nothing had happened. Of course, there is not a variable called S in the source code module she is in, perhaps it was hotfix refactoring after the exception was thrown.

Several versions later, in a code optimization, the u became an attribute of the reference type. When she first arrives at the new source family, she sees a familiar new member in the class property.

“Ah, U.”

“Ah, S.”

With one voice, they said each other’s names.

END

Afterword.

This is the first novel in the author’s blog, and it is also an open source novel. Comments and suggestions are welcome on Github 😀