PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Memory access slowdown?

Hi,

I having strange behaviors with my PPL program and would like to ask for expert's advices.

It is very strange and consistent. Is it possible _strdup() function is acting weird? I have been working on this issue for weeks and I even rewrote the whole compiler and interpreter from scratch and it is still happening.

Are there any rules in C (and PocketPC) of things not to do and things to do when assigning memory? I have tried hundreds of different approaches to almost everypart involve in the compiling and interpreting.

I can run a PPL program and the minute I add a couple lines, it gets slow.

I know my message might be unclear, I have a hard time understanding the problem myself.

Regards,
Kornalius
Well, _strdup() is just a malloc and memcpy. And if you are allocating and/or deallocating lots of small memory blocks over and over per application loop, that can seriously slow down a program on a PPC.

It would be better if you were to allocate the memory initially, then copy the appropriate blocks of memory during the applications main loop. Rather than trying to create and free off the heap dynamically. That's what I would do anyway. Then you cut a lot of the performance overhead that usually arises from any mem-related functions.

Unless yer dealing with a huge gob of memory, then yeah on a PPC yer prolly better off dynamically allocating a huge data block so that you keep yer application's requirements down.

JJB
Is there any way to defragment the memory after having used many little blocks?

Regards,
Kornalius
There is no way. If you were to defragment it, your existing pointers would point to something else (or nothing).
Yeah but what about defragmenting memory blocks that are not assigned?
Defragmenting means moving used blocks around to merge unused ones.

Unused blocks are automatically merged when you free memory.
k, thanks!
Would this be the case for files system memory as well as program memory?

I can see how a file system would get fragmented, and how a linked list style could get fragmented, but individually alloc'd items would never get fragmented themselves - right? Otherwise, addressing them would be quite a task.

So, I assume you're talking about a collection of alloc'd items that have been fragmented.

Why would this be a problem, unless it's something to do with caching? It seems like it would take just as long to address an item whether it's at location X or Y.
Im not sure this is really appropiate to what you are doing, and its sort of what JongJungBu was saying, but could your program allocate a heap at the program intiialisation, and then have your own memory class that will allocate memory on that heap. (Using a list of pointers) This get rid of the memory fragmentation problem, since all of the memory is allocated from the beginning.
kornalius wrote:Yeah but what about defragmenting memory blocks that are not assigned?

Can be easily done, but depends on how sophisticed you want to get. Given you know where the pointers are referenced from in your data structures, it should be pretty straight-forward to create a mark-and sweep type of garbage collector.

1. you have a memory pool of fixed sized pages from which you allocate your strings from.
2. You just allocate strings by finding an appropriate block with enough empty memory, from which you allocate the actual content buffer.
3. Whenever you don't have empty space, you make a decision if you want to try to compact existing space or want to allocate new space.
4. When trying to compact (ie garbage collection), you first identify areas in your heap that can be reclaimed, and then do a second iteration in which you consistently copy life content down, updating pointers to strings as necessary).

Early Java (and AFAIK also VB implementations) were storing not direct pointers to the character buffer, but pointers to constant sized descriptor objects which were pointing to the actual character buffer. In this case, you can have a simple memory pool for the decriptor structures and when you have to update pointers during the collection phase you know that they are all coming from the set of decriptor objects.

Anyway, not sure if I was making myself clear here.

Good luck!

- HM


PS: Just following recent posts around PPL, it looks to me that you find yourself more and more moving towards a CLR/JVM style model. Similar to how you moved away from the direct code generation to an interpreted platform-independent representation. So maybe reading up on the internals of these platforms will give you a good idea what common approaches to all these types of problems are.
Thanks, I have experimented with some of the suggestions posted in this thread and it does make a big difference!

Regards,
Kornalius
simply allocate the maximum amount of memory your app will need right at startup then free it. this will ensure there is a place in memory that is at least as big as your requested buffer size and tell the windows memory subsystem to clean up if needed (defrag). it will also ensure your app won't run out of memory. this allows you to make many little allocations during your program, then every x seconds/allocation/whatever alloc and free the memory again to clean up, maybe inbetween a level load or etc when you're not using any or little memory.