Serious Issue with accumulating memory

Started by Hardcoal, September 04, 2017, 17:55:21

Previous topic - Next topic

Hardcoal

one of my biggest problems in my editor is that when i create an object lets say a missle.. and when it explodes.. and i want to free his memory space
i seem to fail.. and the memory keeps accumulating..  and getting larger..

I dont know what im doing wrong.. and how correctly to delete a complex object..

each object i create is made of a type and in the type there are sub types..  and functions and so on.. so its very complex..

here is my main element Type which each element has this as basic core..


Type Element_Class

   'Media Identity
    Field Name:String
Field FileName:String       'Media Filename 'Also Prefab Filename
Field PrefabFilename:String   'Prefab Case
Field MediaType:Type_Class   'Media Type
Field ExtraPath:String         'Extra path for loading element maybe cancle
Field EntityOrder:Type_Class = Typ.EntOrder_Normal   'Front/Normal/Back (maybe add more)
Field Info:String   'Info about this element. Good for Element case
Field Tag:String    'not in use yet and not saved

   'Family
   
   'Parent
Field Parent:Element_Class

   'Sons
Field Sons:TList = CreateList()
Field ActorsList:TList = CreateList() 'This is for play only. not in use atm [to here will be copied all the actors that are played]

   'Element Behavior
    Field MainGroupClass:ClassExtender_Class = New Group_Class

   'The Element Current Collision Group       [For Collisions and Layers..] 'I might not use the non physical collisions at all..
Field ElementGroup:ElementGroup_Class

   'Media MedExtent
Field MedExtent:Object   'maybe cancle and only use class

   'Element Message
Field ElementMessage:String

   'Physical!
Field Physical:PhysicsPart_class

   'Handles
Field Handle       'Pivot/3D Handle
Field SpecialHandle   'For Specific, Image ETC..

   'Flags
     
Field IsPhysical_flg       'This will be cancled and replaced by check if Physical is null
Field IsLockedToCam_flg       'Means it moves with the cam!
Field IsLocked_flg   'Lock Element, prevents from being selected on the mapeditor, Not in use yet.
Field IsClosed_flg     'a General is closed flag mainly used for declaring a closed poly or general closed referencer!
Field IsFlipped_flg     'Flag that tells if the mesh is flipped or not. dont cut my balls please im evil
Field MeshWasFlipped_flg    'This is indicator that the mesh is flipped. no need save
Field NeedLoadingFlg   'Means this Element need to be loaded. (Still not in use)
Field LightMesh_flg     'If the Mesh is being Lighted!
Field IsPickable_flg = True   'determain if to turn on entity pick mode
Field IsAnimated_flg   'Means it has animation. this turned on on load
Field IsDismantable_flg     'This Flag indicates that this element should be dismantled after load.
Field PlayMediaForces_flg = True    'Play Media Forces or not..

Field IsHidden_flg     'Is Invisible but Played  [maybe change to IsInvisible] 'maybe is hidden only for map use
Field IsDisabled_flg     'Element Is Disabled from the Engine. [for editing purposes]

Field IsActive_flg = True     'maybe unite IsActive,IsHidden,IDisabled..
Field WorkOnlyWhenInDistance_flg   '[not in use atm] Not sure ill use it.. maybe i will only use entity distance to decide which to play.. or EntityVisible option
Field IsGeneralElement_flg   'General Game Element.  [This Flag is not Saved, For now it mainly used for MapEditor Undo Purposes]

Field IsGlobal_flg   'if its global it uses translate entity movement
  Field IsTemp_flg   'Medias that only created while running
Field ToBeLoaded_Flg = True   'This mean that when loading a map, reload that..
Field TobeDeleted_flg       'This is a flag the indicates this element should be deleted. this is used when engine runs in the end so the element play process loop will end.
Field ExportVariable_flg   'When you export game, If to create Variable for this.
Field DoNotDisplay_flg   'This is for medias on media bank.. [test]

Field KeepIsDisabledValueFlg   'This is for the MapEditor Private Edit for Temp keeping the Value
Field GroupLocked_flg   'Maybe Cancle. For Mapeditor use This is for groups but i want it for private as well
Field IsMissing_flg   'This Flag turn on when a Media is missing.

   'Orientation
Field XPos:Float
Field YPos:Float
Field ZPos:Float
Field Pitch:Float
Field Yaw:Float
Field Roll:Float
Field ScaleX:Float = 1
Field ScaleY:Float = 1
Field ScaleZ:Float = 1

   'Looks  'This may be only for mesh etc..
Field Alpha:Float = 1
Field ColorR:Float = 255
Field ColorG:Float = 255
Field ColorB:Float = 255

   'Text (Both For 3D Text and Simple (for now)) 'Use SpecialHandle as Font Handle!! 'Use Work Colors to get Color
Field Text:String 'for now i leave it for text3d
Field FontSize:Float = 20

   'Animation--'
Field FramesAmnt

   'Network 'Extand that too if needed
Field NetworkUpdate_flg 'Whether to Send data Via net or not!
   'Field GNetObject:TGNetObject 'maybe turn into a class.. and cancle Need to be created maybe when network on (under test)

Field GeneralValueUse:Float

   'Frictions [non physical atm]
Field Friction:Float = 0.98

   'FX
Field EntityFX = 35 'On Png Case it should be 3 or 1

   'Element Texture 'maybe again do texture filename alone..
   'Field Texture:Texture_Class
Field TextureHandle
Field TextureFilename:String

   'Animation  [this will be moved into texture class player..
    Field AnimSpeed:Float = 1
Field CurrentFrame:Float 'Also Starting Frame
Field AnimDirection = Direction_Forward 'this should be cancled.. 

   'Temps [Not for save]
   
Field CreationTime
Field UndoList:TList = CreateList() 'The list of undo's

   'Move Forces
Field XForce:Float
Field YForce:Float
Field ZForce:Float
   'Angle Forces
Field PitchForce:Float
Field YawForce:Float
Field RollForce:Float
   'Scale Force
Field ScaleXForce:Float
Field ScaleYForce:Float
Field ScaleZForce:Float

Field BrowserALHandle
Field PickerHandle

Field ElementWasPlayed_flg 'marker for played element on mapeditor..

Method New()

Self.MainGroupClass.Name = "ElementMainGroupClass"
Self.MainGroupClass.ClassElement = Self
Self.MainGroupClass.ClassTypeName = "Group_Class"

End Method

End Type



any suggestion will be greeted ..
Code

Henri

Hi,

there might be a cyclic dependency, where reference count of the object never falls to zero, hence the object is never collected.
Usually, in complex types it's better to have a 'Free / Remove' - method, where all the necessary fields are assigned with 'null', so that there are no unwanted references of said object.

In MaxIDE See 'Help / Language / Advanced topics / Memory management'

-Henri
- Got 01100011 problems, but the bit ain't 00000001

markcwm

Try setting up tests for every type of object you have and their functions so you can find bugs. I'll be doing the same myself soon.

Hardcoal

Thanks Henri . Again you are a great Help!
Code

Hardcoal

#4
I tried mark.. but its too complex to try each type by itself cause they wont work.
anyway.. Herni Got me on the right track so Im happy.

every time i think im a good programmer i get reminded how newbie i am.
but i have many gaps in my knowledge and skills so i admit
Code

Krischan

Like Henry said - I always nullify every single object I don't use anymore and use the GCcollect() Garbage Collection once in a loop. The Garbage collection does not always clean up the consumed data instantly but if you nullify it first then it gets deleted instantly, as far as I've observed in a similar situation. But don't put the GCcollect() command inside a Type loop, just use it once where the Flip command is located and it'll do the job.

A method to locate the memory leak is to start with a few fields and then repeat adding another one and watch the memory in a testbed each run. So you could identify the problem, this takes a lot of time but it is worth.

Another problem are Globals in a Type field if I remember this problem correct. I try to avoid and replace them with Fields where I can. They are even harder to "destroy".
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Steve Elliott

I think it's best to think of adding and subtracting as you code...To add and add and add - and then try and trace the deletes is a nightmare later on.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Hardcoal

I never use global inside a type.. only fields..

I also think i can built a command that will make sure everything in a type gets deleted..
Ill see to it soon.

but so far.. henri just solved my problem :)
Code

col

Hiya,

I'm sure you'll find the culprit of your issue with the advice given :)

QuoteI always nullify every single object I don't use anymore and use the GCcollect() Garbage Collection once in a loop
I see most larger projects have code which has 'managers' for their entity types which are pretty much managing instances, which translates to managing memory.
So my curiosity is to ask 'Does this mean that you may prefer not to have a garbage collector memory management system at all?'
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."