The present invention relates to resource allocation in connection with software development. More particularly, the invention relates to a method for allocating resources such as memory in a process of steps in order to trace resource leaks such as memory leaks in software development.
In computer science, a memory leak is a particular kind of unintentional memory consumption by a computer program where the computer program fails to release memory when no longer needed.
A memory leak has similar symptoms as a number of other problems and can generally only be diagnosed by a software developer with access to the computer program source code, hereinafter referred to as computer program code.
A memory leak can diminish the performance of a system by reducing the amount of available memory. Memory allocation can be a component of the operating system or a component in a process or program, therefore the result of a memory leak is usually an ever growing amount of memory being used by the system as a whole or by the erroneous process or program.
Eventually, in the worst case, too much of the available memory may become allocated and all or part of the system or device stops working correctly and/or the applications fails. Memory leaks can cause more serious implications if:
Memory leaks are a common error in software development, especially when using programming languages that have no automatic garbage collection i.e. automatic memory management, such as C and C++. Typically, a memory leak occurs because dynamically allocated memory has become unreachable.
If a program has a memory leak and its memory usage is steadily increasing, this will usually not be noticeable immediately. Almost all systems have a certain amount of available memory, which can be consumed by programs. Eventually, the available memory, e.g. Random Access Memory (RAM), may run out. This may have one of two effects:
As mentioned above, a memory leak typically occurs because dynamically allocated memory has become unreachable. Without access to the computer program code, a software developer noticing symptoms in a process or program can only guess that there might be a memory leak.
Therefore, memory leaks that occur during software development pose a problem. Detecting that a memory leak is present is relatively easy for a skilled person in the art, i.e. a software developer, however locating which part of the computer program code that is responsible for freeing the “leaking” memory can be relatively hard to do and extremely time consuming for software developers.
Other resources than memory, e.g. file handlers, ports or sockets, could also be handled in the same manner.
Existing solutions for identifying memory leaks have the drawback of just being able to identify the file and line of the actual memory allocation call. This information is insufficient since the software developer also needs to identify the computer program code that initiates the memory allocation call which may differ from the computer program code that in fact uses the allocated memory.
Furthermore, existing solutions for identifying memory leaks have the drawback of being too complex to be implemented and used in an embedded system, such as a mobile device.
US 2005/0144410 discloses a method and a system for a data structure within a block of allocable memory of a memory structure such as a heap which is used to identify the function that is responsible for causing memory problems such as random overwrites, buffer overruns, double or multiple “frees”, and the like. The data structure can use the return address of the function that allocates or frees the allocable memory block to identify the caller. An identifier for the function is generated and stored in the allocable memory block.
Even though the above prior art provides a method for managing memory leaks, the disclosed method does not identify the computer program code that initiates the memory allocation call. Instead the method identifies the computer program code that in fact uses the allocated memory. Hence, the above method still requires locating the computer program code that initiates the memory allocation call and it remains a problem to provide a method for identifying the computer program code that initiates the memory allocation call and that solves above problems.
U.S. Pat. No. 6,658,652 discloses a method and a system for detecting memory leaks in an object-oriented environment during real-time trace processing. During the profiling of a program executing in a data processing system, a profiler processes events caused by the execution of the program, and the profiler maintains a profile data structure containing execution-related metrics for the program. The execution-related metrics may include object allocation and deallocations metrics that are associated with object processing initiated on behalf of an executing method. An object allocator allocates objects during the execution of the program and modifies object allocation metrics in the profile data structure. Object metrics are stored in a particular location and a pointer to that location is stored in a hash table associated with the object's ID. In another embodiment the pointer to the location is stored in a shadow heap in the same relative position as the position of the object in the heap. The object allocation metrics and the object deallocation metrics may be compared to identify memory leaks.
Even though the above prior art method provides a method for managing memory leaks, the mentioned method does not identify the computer program code that initiates the memory allocation call. Instead the method identifies the computer program code that in fact uses the allocated memory. Hence, the above method still requires locating the computer program code that initiates the memory allocation call and it remains a problem to provide a method for identifying the computer program code that initiates the memory allocation call and that solves the above problems.
Furthermore, the above prior art method does not provide a possibility for the software developer to, in the computer program code, free the memory himself.
The method described herein relates to identifying a resource allocation in computer program code. The method comprises the steps of identifying computer program code that initiates a resource allocation process, generating a certificate for the identified computer program code and storing the certificate as a parameter in the computer program code initiating the resource allocation process and in a table which further comprises adhering file and line data of the computer program code that initiates the resource allocation process, and performing a resource allocation call including the certificate.
Further, the resource allocation call step comprises performing a function call.
By allocating resources in such steps, the part of the computer program code that initiates the resource allocation process can be easily traced in case of a resource leak. The resources may comprise memory, file handlers, ports or sockets etc.
In another embodiment, a function returns a pointer via a memory handler to newly allocated memory whereby a debug heap implementation wraps the function inside a macro. The macro can then find the adhering file and line data as the macro is expanded at the place of the memory allocation.
In another embodiment, a test procedure establishes if memory is leaking in the computer program code. The test procedure indicates a memory leak being present in the computer program code and the memory leak is traced via the stored certificate to the responsible computer program code that initiates the memory allocation process.
An advantage of a method that allocates memory in such steps according to embodiments of the invention is that the method enables easy trace of a memory leak in computer program code. Furthermore, embodiments of the invention enable linking the computer program code that initiates the memory allocation process to the actual memory allocation.
A further advantage of a method that allocates memory in such steps according to embodiments of the invention is that the method is also applicable for programming languages for use in hardware development.
Moreover, a trace environment, which is a tool for creating structured trace output that can be displayed and analyzed, is not necessary anymore.
Further, the computer program code may comprise an operating system, an application, an algorithm, and a driver.
In yet another embodiment, a data processing system configured to perform the steps of the above method further comprises a system memory which in turn comprises a Read Only Memory and a Read Access Memory including a heap Application Programming Interface.
Embodiments of the invention will now be described more in detail in connection with the enclosed drawings.
To facilitate an understanding of the following embodiments, many aspects are described in terms of sequences of actions that can be performed by elements of a computer system. For example, it will be recognized that in each of the embodiments, the various actions can be performed by specialized circuits or circuitry (e.g., discrete logic gates interconnected to perform a specialized function), by program instructions being executed by one or more processors, or by a combination of both.
Moreover, the sequences of actions can be embodied in any computer readable medium for use by or in connection with an instruction execution system, apparatus, or device, such as a computer based system, processor containing system, or other system that can fetch the instructions from a computer readable medium and execute the instructions.
As used herein, a computer readable medium can be any means that can contain, store, communicate, propagate, or transport the program for use by or in connection with the instruction execution system, apparatus, or device. The computer readable medium can be, for example but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, device, or propagation medium. More specific examples (a non exhaustive list) of the computer readable medium can include the following: an electrical connection having one or more wires, a random access memory (RAM), a read only memory (ROM), an erasable programmable read only memory (EPROM or Flash memory), and an optical fiber.
The system memory (500) further comprises applications (503) and program data which typically includes one or more heaps (504) wherein a heap is a pool of unused memory. Furthermore, the system memory (500) also comprises an operating system which includes the heap API (502), algorithms, and drivers.
The method described herein relates to allocating memory in a process of following steps, wherein a first step is performed by computer program code that initiates a memory allocating process and a second step is performed by an actual memory allocation. By allocating memory in such steps, the part of the computer program code that initiates the memory allocation process can be easily traced in case of a memory leak.
More specifically, the process of allocating memory is performed in following steps:
A correct certificate, as referred to in step 2, is a certificate which is present in the database. Therefore, a memory allocation call with a certificate ID which has not been allocated before is incorrect and not valid. In practice, this means that computer program code that needs to allocate memory, but does not free the memory itself, has to be provided with a certificate for memory allocation. However, whether all computer program code or only parts of it should be provided with certificates for memory allocation is optional and is more of a design question for the specific case. The computer program code requests that the certificate should free the memory. However, the certificate itself does not free the memory. Instead a special function, e.g. HEAP_FREE, acts as a stand-in function for the normal free function. If a memory leak is present in the computer program code, the culprit can be found by finding the part of the computer program code that initiated a memory allocating process, which in turn caused the leaking memory, via the certificate.
Furthermore, additional information exists that can be associated with a certificate, for example:
If the certificate is constrained to a one-time only allocation, the certificate and the matching memory block which should be freed can be excluded from the database at the first function call to HEAP_FREE.
If the certificate allows for several allocations then the certificate is not excluded from the database since it is needed for possible further memory allocations and memory frees. In this case, a new function should be used that deallocates the certificate when it is no longer needed.
Example 1 illustrates an exemplary C computer program code version of the memory allocation process according to embodiments of the invention in the block of computer program code. Example 1 is provided to illustrate an actual block of computer program code using embodiments of the invention. More specifically, when including an external library such as AllocDocument. The // denotes comments to the computer program code.
HEAP_ALLOC and HEAP_UNTYPED_ALLOC are macros. The properties of macros enables the macro to expand in its place in the computer program code which renders the _FILE_and_LINE_macros (that are included in the HEAP_macros) to expand correctly and to be inserted into the debug heap.
AllocDocument is an example of an implementation of including an external library, e.g. often such libraries encapsulate memory allocations in a function in order to facilitate porting of the code. In this case, only one malloc call for freeing memory needs to be replaced instead of all portions of the computer program code where memory allocations are occurring.
Furthermore, AllocDocument could also be implemented as a macro and in this case go up one step in the function call chain. With this method one obtains more information about where the memory allocation is occurring while still staying inside the library.
It should be emphasized that the term “comprises/comprising” when used in this specification is taken to specify the presence of stated features, integers, steps or components but does not preclude the presence or addition of one or more other features, integers, step, components or groups thereof.
Number | Date | Country | Kind |
---|---|---|---|
07107800.0 | May 2007 | EP | regional |
Filing Document | Filing Date | Country | Kind | 371c Date |
---|---|---|---|---|
PCT/EP08/55477 | 5/5/2008 | WO | 00 | 5/6/2010 |
Number | Date | Country | |
---|---|---|---|
60917107 | May 2007 | US |