I programmed an auto-crafting software that is very basic at the moment and crafts every craftable in the ME in steps of 20 until they reach e.g. 100 items. Therefore it pulls the itemlist from the ME and every x secs the craftables list from the ME.
It stores all informations in tables and prints failed crafting requests.
So to the problem: It works perfectly most of the time and I do not know when exactly this changes but suddenly the free RAM drops a lot and keeps getting lower until it crashes. Normally i have it at around 2.9MB free RAM.
But I can't see where in my code this happens, if I free up all my tables that are global for debugging purposes almost no RAM gets free.
Hopefully someone sees the error in my program, feel free to use it if you think you need it
I always run it with: while true do p.produce() component.gpu.set(150,50,tostring(computer.freeMemory())) if computer.freeMemory()<80000 then break end end
---config section
local version="0.9.9.5b"
local production_number=100
local overproduction=production_number/5 --actually steps
local cpus=25
local craft_update_time=300
local update_time=30
---
local component=require("component")
local serialization=require("serialization")
local computer=require("computer")
local p={} --functions
local me
local me_address
tmp={}
for a,b in component.list("me_controller") do tmp[#tmp+1]=a end
tmp2={}
for i=1,#tmp do tmp2[i]=component.proxy(tmp[i]) end
for i=1,#tmp2 do if #tmp2[i].getItemsInNetwork()>0 then me_address=tmp[i] end end
tmp=nil
tmp2=nil
inProgress={}
index={} --remove local for debugging
craftables={}
items={}
local ctime=computer.uptime()
local chtime=ctime-180
local amount=1000
failed={}
local last_i=1
local blacklist={}
--add saving to file and starting from file --> not working because of task-object
--add function to add to blacklist or remove from it
--generally: return table is a security risk! (or debug possibility...)
local function getIdent(t)
ret=""
for a,b in pairs(t) do
if a~="size" then
ret=ret..a.."="..tostring(..","
end
end
ret=ret:sub(1,ret:len()-1)
return ret
end
function p.initialize(handler)
f=handler
f.addTask(p.produce)
end
function p.produce()
me=component.proxy(me_address)
if me==nil then
print("no me, trying again")
else
if check_time()==true then
p.craftables_pull()
if check_production()==true then
p.items_pull()
local it=1
for i=last_i,#craftables do
if #inProgress>=cpus then
break
else
local name=craftables[i].getItemStack()
local tmp=items[items.ident[getIdent(name)]]
if tmp then
if tmp.size<production_number then
local tmp2
tmp2=getIdent(tmp)
if index[tmp2]==nil and blacklist[tmp.label]==nil and blacklist[tmp2]==nil then
inProgress[#inProgress+1]=craftables[i].request(20)--production_number+overproduction-tmp.size)
if inProgress[#inProgress].isCanceled()==true then
print(tmp2)
table.remove(inProgress,#inProgress)
--add second chance (lower production amount)
else
index[#index+1]=tmp2
index[tmp2]=#index
end
end
tmp2=nil
end
tmp=nil name=nil
it=i
end
end
end
last_i=it it=nil
if last_i==#craftables then
last_i=1
end
end
end
end
--f.addTask(p.produce)
if not f then
os.sleep(1)
end
end
function p.listTasks()
return inProgress
end
function check_time()
if chtime<computer.uptime()-update_time then
chtime=computer.uptime()
return true
else
return false
end
end
function check_production()
if #inProgress==0 then
return true
else
local tmp={}
for i=1,#inProgress do
if inProgress[i].isDone()==true or inProgress[i].isCanceled()==true then
if inProgress[i].isCanceled()==true then
print(index[i])
else
end
index[index[i]]=nil
tmp[#tmp+1]=i
end
end
for i=1,#tmp do
table.remove(inProgress,tmp[i]-i+1)
table.remove(index,tmp[i]-i+1)
end
tmp=nil
if #inProgress<cpus then
return true
else
return false
end
end
end
function p.craftables_pull()
local ctime2=computer.uptime()
if ctime2-ctime>craft_update_time or ctime2-ctime<0 or #craftables==0 then
ctime=ctime2
craftables=nil
craftables=me.getCraftables()
end
ctime2=nil
end
function p.items_pull()
items=nil
items=me.getItemsInNetwork()
items.ident={}
for i=1,#items do
local j=items[i]
local name=getIdent(j)
items.ident[name]=i
name=nil j=nil
end
end
function p.listFailed()
return failed
end
return p
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
Hi guys,
I programmed an auto-crafting software that is very basic at the moment and crafts every craftable in the ME in steps of 20 until they reach e.g. 100 items. Therefore it pulls the itemlist from the ME and every x secs the craftables list from the ME.
It stores all informations in tables and prints failed crafting requests.
So to the problem: It works perfectly most of the time and I do not know when exactly this changes but suddenly the free RAM drops a lot and keeps getting lower until it crashes. Normally i have it at around 2.9MB free RAM.
But I can't see where in my code this happens, if I free up all my tables that are global for debugging purposes almost no RAM gets free.
Hopefully someone sees the error in my program, feel free to use it if you think you need it
I always run it with: while true do p.produce() component.gpu.set(150,50,tostring(computer.freeMemory())) if computer.freeMemory()<80000 then break end end
Link to post
Share on other sites