Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Tharotiger

Program runs out of Memory

Question

Hello everyone!
 
I'm almost new to OpenComputers. Now i wrote a program for a Tier 2 Computer with 2 Tier 2.5 Memorys inside.
 
After a while the Program Crashs with Out of Memory. If i restart the Program, a few seconds later the whole Computer crash into Bluescreen.
 
Is there a method to get around this?
 

-----------------
--Configuration--
-----------------
local programName = "Fluid Monitor"
local programVersion = "v0.02"
local latenz = 0.5
local showRAM = false

--Farben des Bildschirms
local mainBackground  = 0x0000FF
local mainForeground  = 0xFFFFFF
local titleBackground = 0x333333
local titleForeground = 0xFFFFFF
local labelBackground = 0xAAAAFF
local labelForeground = 0xFFFFFF

--Adressen der Tanks
local tankAddresses = {
	"ff6bd79a-16b7-439f-8301-94ad5b1f36da",
	"9296cb58-590a-4a7e-83cd-0ed335e10515",
}

----------
--System--
----------

--Componenten
local term = require("term")
local c = require("component")
local gpu = c.gpu
local com = require("computer")
local shell = require("shell")

--Informationen der Tanks werden geupdated
function tankUpdate()
	tank = {}
	for key,value in pairs(tankAddresses) do
		local t = c.proxy(value)
		local tInfo = t.getTankInfo()
		local tAmount = tonumber(tInfo["amount"])
		local tCapacity = tonumber(tInfo["capacity"])
		
		tank[key] = {}
		tank[key].amount = tAmount
		tank[key].capacity = tCapacity
	end
end


function fillBar(x, y, amount, capacity)
	--Prozent errechnen
	percent = amount / (capacity / 100)
	percent = math.floor(percent / 10)
	
	gpu.setBackground(0x00FF00)
	gpu.fill(x,y,percent,1," ")
	gpu.setBackground(0xFF0000)
	startPointRedX   = x + percent
	gpu.fill(startPointRedX,y,10-percent,1," ")
end

function tankDisplay(label, x, y, tank)
	gpu.setForeground(labelForeground)
	gpu.setBackground(labelBackground)
	term.setCursor(x,y)
	term.write(label)
	fillBar(x, y+1, tank["amount"], tank["capacity"])
	gpu.setBackground(mainBackground)

	percent = tank["amount"] / (tank["capacity"] / 100)
	percent = math.floor(percent)
	
	term.setCursor(x,y+2)
	term.write("P: "..percent.."%   ")
	term.setCursor(x,y+3)
	term.write("C: "..tank["amount"])
	term.setCursor(x,y+4)
	term.write("M: "..tank["capacity"])
end

gpu.setBackground(mainBackground)
term.clear()

function displayUpdate()
	tankUpdate()

	--Titelleiste
	gpu.setForeground(titleForeground)
	gpu.setBackground(titleBackground)
	term.setCursor(1,1)
	term.clearLine()
	term.write(programName.." "..programVersion)
	
	--Tankanzeigen
	tankDisplay(" Gülle    ", 3, 3, tank[1])
	tankDisplay(" Gülle    ", 15, 3, tank[2])
	--tankDisplay(47, 3, allTankTable)

	
	--Informationen zum RAM
	if showRAM == true then
		term.setCursor(1,24)
		term.write("FreeMemory:  "..com.freeMemory())
		term.setCursor(1,25)
		term.write("TotalMemory: "..com.totalMemory())
	end
		
	gpu.setForeground(0xFFFFFF)
	gpu.setBackground(0x000000)
end

while true do
  displayUpdate()
  os.sleep(latenz)
end

 
As you can see in the end of the Code i've added Code to Show the RAM usage. You can see that the RAM is filled over the Time.
 
 
PS: I've startet a new Thread because the other one a few days ago is Marked as answerd!

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You are looping a creation of a table in your tankUpdate function, which should be handled by the GC, but is unnecessary.

Replace line 36 with:

if not tank then tank = {} end
and line 43 with:

if not tank[key] then tank[key] = {} end
Alternatively create the structure once outside that loop if the tank components are fixed.
Link to post
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.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   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.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.