Você está na página 1de 12

Koslibpro Project

General
I see, plenty of times, computers using high-speed CPUs, huge capacity RAM and generally the best hardware they can integrate, but after a while of hard usage, they get stack. And in case I open the Task Manager and see what is going on with system processes and apps open, I can suppose that the problem is coming from the multy-opening of programs and applications. Cause when we open, let's say 10 programs, by default they grab some memory space and dont let it away if they dont get closed. Very small is the list with programs getting idle when they are not used, and they are all big apps that most of home users do not even know! Thus, I thought it would be a valuable idea to make some research on how can we reduce to the less memory space grabed, when apps dont used.

Beginning
See how many apps are opened, but it is not possible all of them to be in use! Thus they use memory space without reason! Wouldnt it be better if there were a special idling app, for gaining more memory space..?

Lets get started;)


With the next part of C code, I assume that in Linux I can limit the processes resources. This is my main workon code.I've not created it by myshelf,this is what i found during Googling and seted-up my workjob on this..
#include <sys/types.h> #include <sys/time.h> #include <sys/resource.h> struct rlimit r = { 0, 0 }; if (setrlimit(RLIMIT_NPROC, &r) != 0) { /* setrlimit( ) error */ } if (fork() == -1) { perror("fork"); }

To alter the size of allocated memory: The memory allocated might be insufficient or excess sometimes in both the situations we can change the memory size already allocated. ptr=realloc(ptr,newsize); Thus, if we create a timer and say: If(idle=60) { /*and here set the cut of CPU and RAM resources*/ } Else { Break; } I think we can achieve hardware usage reduction according to this app/program and concentrate the (now)free amount of memory and CPU on the current-using program.

SOME MEMORY RE-ALLOCATION

Most applications request memory from the heap when they are running It is possible to run out of memory (you may even have gotten a message like "Running Low On Virtual Memory") So, it is important to return memory to the heap when you no longer need it
In other words, this is C++ is something like this:

a = new int[3]; /**a and a[0] is the way to to refer to the same value in this situation*/ *a = 300; *(a+1) = 301; *(a+2) = 302; delete[] a;

See the right picture>>

My comments till here.. I suppose we have to make our best in order not to exist any memory leaks, cause they are gonna fill up all the system with useless cache bytes. Currently, my main assumption on how should I design my app, I decided: 1. To create an app which will be forced with a special timing engine(this will be included into the code).At first, this section of the app will read the time-idle(which will be set to 1-2 secs after minimizing the process) . 2. When the timer says that the main section of the app is ready to be activated, then the code will actually cut the resources given to this app, thus its gonna use much less memory (the less achieved memory will be the target)so that freed RAM will be dedicated to the opened program-app) 3. After the memory-reduction, in case the app we minimized is opened up again, my app is gonna restore all the amount of memory needed, so that it will work properly.

What is next,is my main code i've made for the Sciense Fair Project,of course helped by Googling;)

#define NULL (void *)0

typedef struct hdr { struct hdr *ptr; unsigned int size; } HEADER; extern HEADER _heapstart, _heapend; extern void warm_boot(char *str); static HEADER *frhd; static short memleft; /* memory left */ void free(char *ap) { HEADER *nxt, *prev, *f; f = (HEADER *)ap - 1; memleft += f->size;

if (frhd > f) { /* Free-space head is higher up in memory than returnee. */ nxt = frhd; /* old head */ frhd = f; /* new head */ prev = f + f->size; /* right after new head */ if (prev==nxt) /* Old and new are contiguous. */ f->size += nxt->size; f->ptr = nxt->ptr; /* Form one block. */ } else f->ptr = nxt; return; } nxt = frhd; for (nxt=frhd; nxt && nxt < f; prev=nxt,nxt=nxt->ptr) { if (nxt+nxt->size == f) { nxt->size += f->size; /* They're contiguous. */ f = nxt + nxt->size; /* Form one block. */

if (f==nxt->ptr) { nxt->size += f->size; nxt->ptr = f->ptr; } return; } } prev->ptr = f; /* link to queue */ prev = f + f->size; /* right after space to free */ if (prev == nxt) /* 'f' and 'nxt' are contiguous. */ { f->size += nxt->size; f->ptr = nxt->ptr; /* Form a larger, contiguous block. */ } else f->ptr = nxt; return; }

char * malloc(int nbytes) /* bytes to allocate */ { HEADER *nxt, *prev; int nunits; nunits = (nbytes+sizeof(HEADER)-1) / sizeof(HEADER) + 1; for (prev=NULL,nxt=frhd; nxt; nxt = nxt->ptr) { if (nxt->size >= nunits) /* big enough */ { if (nxt->size > nunits) { nxt->size -= nunits; /* Allocate tell end. */ nxt += nxt->size; nxt->size = nunits; } else { if (prev==NULL) frhd = nxt->ptr; else prev->ptr = nxt->ptr; } memleft -= nunits;

return((char *)(nxt+1)); } } warm_boot("Allocation Failed!"); return(NULL); } void i_alloc(void) { frhd = &_heapstart; /* Initialize the allocator. */ frhd->ptr = NULL; frhd->size = ((char *)&_heapend -- (char *)&_heapstart) / sizeof(HEADER); memleft = frhd->size; /* initial size in four-byte units */ } I think that's all,thank you for all.

Você também pode gostar