Memory Pool – Manual memory management

About Monkey 2 Forums Monkey 2 Programming Help Memory Pool – Manual memory management

This topic contains 7 replies, has 3 voices, and was last updated by  ivan.velho 2 years, 6 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #4254

    ivan.velho
    Participant

    Is there a way  to manually manage memory, like Pool  in Monkey 1 ?

    Thanks

    #4255

    impixi
    Participant

    IIRC, there is no Monkey1-like Pool class in MX2’s collections library.

    I did some pooling experiments several MX2 releases back. I could not find a solution that performed quicker than run-time Newing of objects, regardless of the quantity (on PC hardware). Memory fragmentation issues, however, are theoretically solved by object pooling.

    Here’s a simple generic object pooling class:

    Note that any objects added to it require a default New() constructor.

    [/crayon]

    I do have another solution somewhere that makes use of objects that implement a “Pooleable” Interface. I’ll see if I can dig it up and post it as another example…

    #4256

    impixi
    Participant

    Here’s the other pooling technique I experimented with:

    [/crayon]

    Objects to be pooled must implement the iPoolable Interface. The pool also requires a “creator function” (basically a constructor replacement).

    This technique affords greater flexibility though at a cost of greater abstraction.

    I used an array instead of a stack for performance testing reasons – surprisingly there was no performance benefit. Maybe I should reimplement it with a Stack…

    #4259

    ivan.velho
    Participant

    My problem is free memory in time the destruction method is called, same problem every garbage collector has.
    But if you want to be faster than a constructor method, I suggest you to create a pool of objects with a recycle method, this method must do the same as a constructor but it will do on an allocated object.
    This is the reason you can’t be faster with any other strategy.

    #4260

    ivan.velho
    Participant

    The limitation of this method is the pool must contain only objects of the same class.
    You cannot do up cast as down cast.

    #4261

    Jesse
    Participant

    I created a simple generic pool class. I think t’s as fast as it can get. it has no limitation on size and has a return to pool function. The only requirements is that all objects to be stored or created must extend StoreObject class:

    [/crayon]
    #4270

    ivan.velho
    Participant

    I think I could use another approach to the problem.

    I can use  SDL/libc to allocate SDL pixmaps.

    These are the major resources , I must study this solution . I believe monkey2 is based on SDL and the low level SDL/libc were provided to give a low level control.

    I think I must study the integration and figure out how to do this .

    Thanks guys for the help.

    #4271

    ivan.velho
    Participant

    There is no problem:

    Namespace std.graphics

    #rem monkeydoc Pixmaps allow you to store and manipulate rectangular blocks of pixel data.

    A pixmap contains a block of memory used to store a rectangular array of pixels.

    #end
    Class Pixmap

    #rem monkeydoc @hidden
    #end
    Field OnDiscarded:Void()

    #rem monkeydoc Creates a new pixmap.

    @param width The width of the pixmap in pixels.

    @param height The height of the pixmap in pixels.

    @param format The pixmap format.

    @param data A pointer to the pixmap data.

    @param pitch The pitch of the data.

    #end
    Method New( width:Int,height:Int,format:PixelFormat=PixelFormat.RGBA32 )

    Local depth:=PixelFormatDepth( format )
    Local pitch:=width*depth
    Local data:=Cast<UByte Ptr>( libc.malloc( pitch*height ) )

    _width=width
    _height=height
    _format=format
    _depth=depth
    _data=data
    _pitch=pitch
    OnDiscarded=Lambda()
    libc.free( data )
    End
    End

     

    Pixmap calls libc.free(data)

    I believe it is not in garbage collector at all.

    I believe Mark used the approach from libgdx (LWJGL)and allocate and delete resources out of garbage collector, through native method.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.