Memory Usage

General FreeBASIC programming questions.
Posts: 1
Joined: May 08, 2006 3:40

Memory Usage

Postby rook9566 » May 08, 2006 4:49


New here. FreeBASIC has given me an opportunity to relive my teenage years when I would sit for hours and program games and such on my C64. I find it relaxing to just play around and see what I can do. The programs I write are never meant for anybody but myself, I doubt anybody would find them interesting.

My latest program is an evolution simulator. I find the topic of evolution amazing, so I am designing my simulator to start 350,000,000 years in the past with a single cell organism and I want to get it to evolve into something more complex. That might sound like something for a huge mainframe, but the little program I am making is actually quite simple in my eyes. But, I guess I am wrong, because after a few centuries the memory on my PC is maxed out. I use the task manager and I can actually watch the memory fill up as my program runs.

I think the problem is I am overly using arrays, because, that is what I know.

Here is a basic synapsis of how I am trying to get the thing to work.

First the program generates a random text string 256 characters long. It might look something like this.


This is a representative of the earth's enviroment, resources, availability of food, temperature, weather and everything else.

Next I generate the first lifeform colony. It basically randomly generates a single character form a-z. Lets say it chooses the character 'k'. This is representative of the lifeform's DNA

Then it chooses a random location for where that colony "lives" in the text string generated earlier. Let's say it chooses the 15th place in the text straing. The 13th place has a 'p' currently in that place. So I have some code that tells me that k is 5 steps from p. The closer the colony's DNA match is to the enviroment, the better the colony will do.

So then I start counting years. Each year, the colony grows an amount based on its DNA match with the enviroment. Each year I kill off a percentage of the colony too. As long as the colony has grows more than it dies, we are OK and the closer the DNA match is the better.

This all works fine. A little buggy, but I am working on that.

The problem happens when I start adding more colonies. Every decade I spawn a new colony. This will be random when the program is done, but for now it is set at 10 years. There is a small chance that the new colony will be a mutant and have a different DNA code than it's parent. The idea is, if the child DNA is better suited to the enviroment, it will do better than the parent and eventually take over. This is the basis of Darwin's evolution. As the program gets more complex, new child colonies may have more than one character in the DNA string. So you will get stuff that looks like this. fhr dhhej djjskie aiejdkeeka etc. My theory is, as time passes, the colonies that most closely match the enviroment will live and the ones that don't will die.

In time I will introduce random events that change the enviroment both on a small and large scale basis. The colonies will have to adapt or die.

OK, that's some geeky stuff, but it gives me something to do.

But, right now I am stuck with memory problems. After only 1000 years, I have 100 colonies and I am using over 650MB of memory. I can't understand why. I had the game set to max out at 2000 colonies, but can't even get close.

Each colony as the following variables to represent it.

A zstring set to 14 characters in length for the DNA string.
A population variable dimmed as short
A location variable dimmed as short

So that doesn't seem like a lot of memory usage does it? 200 shorts and 100 zstrings?

So where is the memory going? Is there any good reading on memory management that you guys know of? I will post the code if you want, but I am kinda embarassed at my poor programming skills. I haven't done much in 20 years.

Oh and by the way, the program is strictly text based. No graphics.

Any help will be much appreciated as I have speant the last 3 days playing with this and it would suck if it can't work.

Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois

Postby cha0s » May 08, 2006 5:27

yeah you need to show code if you want any sort of guidance.
Posts: 82
Joined: Mar 07, 2006 11:28

Postby Dirk_Fist » May 08, 2006 5:41

Sounds like a memory leak. Your allocating memory in a loop and at some point it needs to be released.

Try grabbing a big junk of memory and parceling it out your self


If your allocating implicity, it could be a compiler bug

without seeing the code I can't say
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt

Postby voodooattack » May 08, 2006 13:57

hi :)

I'm very interested in the topic my self..

you might want to check this out:

a very small app, yet really complex/amazing :D

Gene Pool is an artificial life simulation in which populations of physics-based organisms evolve swimming capabilities over time. These organisms are called "swimbots". You can set mate preference criteria and thus influence what the swimbots consider as attractive qualities in potential mates. The most attractive swimbots get chosen most often and so their genetic building blocks propogate to future generations. Eventually, swimbots get better at pursuing each other, competing for food, and becoming babes to other swimbots. Local gene pools emerge which compete for sex and food (for energy to have more sex). Eventually a dominant sub-population takes over.

sounds very close to the idea of your own project :)
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England

Postby yetifoot » May 08, 2006 15:01

I'd love to see this also. I have used one called 'Tierra' before, and have long been interested in this.
Posts: 341
Joined: May 27, 2005 7:01
Location: Canada

Postby Shadowwolf » May 09, 2006 4:43

Yetifoot I'm working on my own version of Tierra. just finished the basic byte code interpreter yesterday.

my little twisted on the original idea is I'm adding a point system for each program running with in the VM every instruct executed cost points and the only way to recover points is to null a cell in memory not owned by the program. along with a big bonuses for Nulling competing programs. the idea is to place a selection force on programs to developing predatory behaviors.

the only sticking point i have right now is an effect way to track owner ship of memorey cells by the program.

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests