Jump to content

Increase memory limit for JavaScript interpreter

Recommended Posts

Hello everyone,

I am developing my own AI based on distribution functions, thus I would like to create and use big array of 8 GB. I already have 16 GB of RAM, thus HW is not problem.

I was able to create empty AI skeleton from PetraBot and run it inside 0ad (the AI does nothing, all units just stand there, no errors displayed in F9 console).

However, I am unable to create a 8 GB array. When I try using following code in the CustomInit() function      

this.myArray = [];
this.myArray.length = 8000000000;
for (var i = 0; i < 8000000000; i++) {
        this.myArray[i] = 1;

I get error in F9 console

error: JavaScript error: simulation/ai/waiting/_waitingbot.js line 47
error: RangeError: invalid array length
error:  m.WaitingBot.prototype.CustomInit@simulation/ai/waiting/_waitingbot.js:47:9
error:  m.BaseAI.prototype.Init@simulation/ai/common-api/baseAI.js:45:2
error:  m.InitGame@simulation/helpers/InitGame.js:80:2

I asked uncle Google and it looks like there is memory limit in JavaScript interpreters. Indeed, if I lower the array size to 1GB, no error happens in the F9 console.

The JavaScript interpreters allow to set the memory limit on command line, for example

node --max-old-space-size=8000 yourScript.js

but since 0ad is written in C++ and runs a JavaScript interpreter internally, I can NOT configure it.

Thus the question: How to increase memory limit for 0ad's JavaScript interpreter?

I am using Ubuntu 16.04 LTS. I compiled 0ad from SVN successfully.

I already read official documentation about AI development under 0ad and I also tried to search this forum, but no luck. I would be really grateful for any advice.

Link to comment
Share on other sites

Hello and welcome to the forum!

Unfortunately I suppose that we can't allocate more than 4GB of memory per game. Because we compile the game and depended libraries (among them SpiderMonkey) as a 32bit application. There're few ways to allocate more, but it costs more and requires low level changes in the game/SpiderMonkey. I think there is a limit of 1 million elements for standard arrays, because usually an element costs at least 4 bytes (standard int32), it means 4GB of memory. You could allocate up to 4 million elements by Int8Array, but it won't solve your problem in common case.

I can suggest to try compile the game in 64bit mode (but I don't know, would it work or not), or to manage AI by a separate 64bit process, or try to optimize your algorithm to fit into 4GB of memory.

  • Thanks 1
Link to comment
Share on other sites

9 hours ago, vladislavbelov said:


I can suggest to try compile the game in 64bit mode (but I don't know, would it work or not), or to manage AI by a separate 64bit process, or try to optimize your algorithm to fit into 4GB of memory.


If you do manage to compile it in 64 bits let us know.

0AD' Js engine runs with Spidermonkey which is a different J's interpreter from node.js. I didn't find any reference to such a memory limit so I guess it's not limited by default but it's limited here because the game only uses up to 4Gigs of Ram on Linux.


Link to comment
Share on other sites

  • 2 weeks later...

Not to be a downer but 0ad is really not a good AI development platform. The current AI interface in 0ad is basically a horrible hack designed specifically to support petra, and petra is a nightmare of spaghetti code. If you're just looking to get into AI I'd suggest looking into Zero-K instead. It's much, much more friendly for AI dev and even has a very competitive AI dev community. I'm also kind of curious as to what you mean by "distribution functions". That's not an approach that I'm familiar with.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

I thought the same as well, but after reading through it, it started becoming clearer. Maybe, I became accustomed to it or the code just has good structure.

Regarding the AI interface, I do not agree with that. The interface just send simulation events and the gamestate. How generic could it be? There is nothing stopping people from writing up a new AI with a whole different structure. All you need to do is listen to these events and PostCommands in response to those.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...