In contemporary computing, garbage collection refers to removing objects from memory once those objects are no longer in use. However, in a scenario in which a framework has managed code (e.g., .Net) and native code, the current managed garbage collection operations do not account for the native memory consumed by the native part of the framework. This can be problematic, as the native code's objects use far more memory than the managed code's objects.
This Summary is provided to introduce a selection of representative concepts in a simplified form that are further described below in the Detailed Description. This Summary is not intended to identify key features or essential features of the claimed subject matter, nor is it intended to be used in any way that would limit the scope of the claimed subject matter.
Briefly, various aspects of the subject matter described herein are directed towards a technology by which memory usage in native code is monitored to determine when a memory usage condition is reached (e.g., memory usage has increased beyond a threshold). When the condition is reached, the native memory requests that managed code perform a garbage collection operation.
In one aspect, the memory usage is only checked occasionally, such as every fifty frames corresponding to activities that may change memory usage. Further, the managed code may only perform the garbage collection when a sufficient number of objects are ready to be collected.
In one aspect, following an initial garbage collection pass, the native code requests an additional garbage collection pass because the initial garbage collection pass may have made other objects ready to be collected. Additional passes are requested in a loop until the managed code decides not to further perform garbage collection, e.g., when not enough objects remain or the number to be collected does not change between collection passes.
Other advantages may become apparent from the following detailed description when taken in conjunction with the drawings.
The present invention is illustrated by way of example and not limited in the accompanying figures in which like reference numerals indicate similar elements and in which:
Various aspects of the technology described herein are generally directed towards mechanism of controlling garbage collection based on actual native memory usage in a managed code/native code framework. In general, a memory pressure mechanism (algorithm) occasionally checks the use of memory by a native code process, and if memory usage has increased beyond a threshold amount, requests a garbage collection operation.
While Microsoft® Silverlight™ (a cross-platform, cross-browser plug-in that may act as a user interface to a media platform for web content) is an example framework in which managed .Net code and native code are used as examples, it should be understood that any of the examples described herein are non-limiting examples. As such, the present invention is not limited to any particular embodiments, aspects, concepts, structures, functionalities or examples described herein. Rather, any of the embodiments, aspects, concepts, structures, functionalities or examples described herein are non-limiting, and the present invention may be used various ways that provide benefits and advantages in computing in general.
In general, to manage the memory, a memory pressure mechanism 112 (algorithm) starts in a listening state as generally represented by the state 221 of
The managed code 104 processes the call and includes garbage collection logic 114 that checks the managed portion of memory (a reference table 116 associated with the managed objects 108 and corresponding native peer objects 110) to determine whether there is a sufficient number of objects to be collected. If not, the memory pressure mechanism 112 goes back to the listening state 221. If so, the garbage collection logic 114 causes those objects to be collected in one pass, and then, because this collection pass may cause more objects to be available for collection, posts an asynchronous message for a next pass; (the memory pressure mechanism 112 goes between a collection state 223 and a triggering state 222 as described below). In the next pass the operation performs another collection if the pending objects to be collected are different from the last collection pass. This loop continues until as also described below until the objects are collected as needed. The memory pressure mechanism 112 then goes back to the listening state 221.
In one implementation, the example steps in the process of
Steps 306 and 308 determine whether the amount of memory being used by the process has increased by a threshold memory increase amount X, (e.g. fifty megabytes, which may be configurable), that is by evaluating the current size compared to the previous size. If so, as represented by step 310, the memory pressure mechanism transitions to the triggering mode 222, where it calls up to the managed side to trigger the possible garbage collection.
In response to the trigger, at step 312 the managed code's garbage collection logic 114 first checks to see if there are more than Y number of objects ready to get collected, (e.g., at least one hundred such objects as tracked in the reference table 116, where Y may be configurable). If not, the collection is not performed, and the memory pressure mechanism returns to the listening state 221. If so, the logic 114 forces a garbage collection (GC.Collect) at step 314, and at step 316 enters a collecting mode (the memory pressure mechanism enters the collection state) and posts a message back to the native code to call back to the managed code.
In the native side, in response to this message, the memory pressure mechanism 112 will post another message to the managed code to ensure that any additional objects that get put into the reference table 116 (as a result of this most-recent garbage collection) can also be collected. Thus, in response at step 318, if the memory pressure mechanism is in the collection state 223 when this message is handled, the memory pressure mechanism returns to the triggering state 222, and calls back via step 310 to the managed code for another garbage collection.
This loop continues until at step 312 the managed side determines that there are no more objects to collect, or the count in the reference table has not changed between the two collections. If either condition is satisfied, the memory pressure mechanism returns to the listening state 221.
In one implementation, another optimization may be included when in the collection/triggering states. Although not explicitly shown in
The invention is operational with numerous other general purpose or special purpose computing system environments or configurations. Examples of well known computing systems, environments, and/or configurations that may be suitable for use with the invention include, but are not limited to: personal computers, server computers, hand-held or laptop devices, tablet devices, multiprocessor systems, microprocessor-based systems, set top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments that include any of the above systems or devices, and the like.
The invention may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, and so forth, which perform particular tasks or implement particular abstract data types. The invention may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in local and/or remote computer storage media including memory storage devices.
With reference to
The computer 410 typically includes a variety of computer-readable media. Computer-readable media can be any available media that can be accessed by the computer 410 and includes both volatile and nonvolatile media, and removable and non-removable media. By way of example, and not limitation, computer-readable media may comprise computer storage media and communication media. Computer storage media includes volatile and nonvolatile, removable and non-removable media implemented in any method or technology for storage of information such as computer-readable instructions, data structures, program modules or other data. Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disks (DVD) or other optical disk storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to store the desired information and which can accessed by the computer 410. Communication media typically embodies computer-readable instructions, data structures, program modules or other data in a modulated data signal such as a carrier wave or other transport mechanism and includes any information delivery media. The term “modulated data signal” means a signal that has one or more of its characteristics set or changed in such a manner as to encode information in the signal. By way of example, and not limitation, communication media includes wired media such as a wired network or direct-wired connection, and wireless media such as acoustic, RF, infrared and other wireless media. Combinations of the any of the above may also be included within the scope of computer-readable media.
The system memory 430 includes computer storage media in the form of volatile and/or nonvolatile memory such as read only memory (ROM) 431 and random access memory (RAM) 432. A basic input/output system 433 (BIOS), containing the basic routines that help to transfer information between elements within computer 410, such as during start-up, is typically stored in ROM 431. RAM 432 typically contains data and/or program modules that are immediately accessible to and/or presently being operated on by processing unit 420. By way of example, and not limitation,
The computer 410 may also include other removable/non-removable, volatile/nonvolatile computer storage media. By way of example only,
The drives and their associated computer storage media, described above and illustrated in
The computer 410 may operate in a networked environment using logical connections to one or more remote computers, such as a remote computer 480. The remote computer 480 may be a personal computer, a server, a router, a network PC, a peer device or other common network node, and typically includes many or all of the elements described above relative to the computer 410, although only a memory storage device 481 has been illustrated in
When used in a LAN networking environment, the computer 410 is connected to the LAN 471 through a network interface or adapter 470. When used in a WAN networking environment, the computer 410 typically includes a modem 472 or other means for establishing communications over the WAN 473, such as the Internet. The modem 472, which may be internal or external, may be connected to the system bus 421 via the user input interface 460 or other appropriate mechanism. A wireless networking component 474 such as comprising an interface and antenna may be coupled through a suitable device such as an access point or peer computer to a WAN or LAN. In a networked environment, program modules depicted relative to the computer 410, or portions thereof, may be stored in the remote memory storage device. By way of example, and not limitation,
An auxiliary subsystem 499 (e.g., for auxiliary display of content) may be connected via the user interface 460 to allow data such as program content, system status and event notifications to be provided to the user, even if the main portions of the computer system are in a low power state. The auxiliary subsystem 499 may be connected to the modem 472 and/or network interface 470 to allow communication between these systems while the main processing unit 420 is in a low power state.
While the invention is susceptible to various modifications and alternative constructions, certain illustrated embodiments thereof are shown in the drawings and have been described above in detail. It should be understood, however, that there is no intention to limit the invention to the specific forms disclosed, but on the contrary, the intention is to cover all modifications, alternative constructions, and equivalents failing within the spirit and scope of the invention.