nsIMemory 编辑

xpcom/base/nsIMemory.idlScriptable This interface represents a generic memory allocator. Inherits from: nsISupports Last changed in Gecko 0.9.6

nsIMemory is used to allocate and deallocate memory segments from a heap. The implementation is free to define the heap. NS_GetMemoryManager returns the global nsIMemory instance.

Method overview

voidPtr alloc(in size_t size); Violates the XPCOM interface guidelines
void free(in voidPtr ptr); Violates the XPCOM interface guidelines
void heapMinimize(in boolean immediate);
boolean isLowMemory(); Deprecated since Gecko 2.0
voidPtr realloc(in voidPtr ptr, in size_t newSize); Violates the XPCOM interface guidelines

Methods

alloc

Allocates a block of memory of a particular size.

voidPtr alloc(
  in size_t size
);
Parameters
size
The size of the block to allocate.
Return value

If the memory cannot be allocated (because of an out-of-memory condition), null is returned. Otherwise, it returns a pointer to the newly allocated memory segment. The result must be freed with a call to free() when it is no longer needed.

free

Frees a previously allocated block of memory.

void free(
  in voidPtr ptr
);
Parameters
ptr
The address of the memory block to free. This may be null, in which case nothing happens.

heapMinimize()

Attempts to shrink the size of the heap. A particular nsIMemory instance may choose not to implement this method.

void heapMinimize(
  in boolean immediate
);
Parameters
immediate
If true, heap minimization will occur immediately if the call was made on the main thread. If false, the flush will be scheduled to happen when the app is idle.

isLowMemory

Determine if we are in a low-memory situation (what constitutes low-memory is platform dependent). This can be used to trigger the memory pressure observers.

Note: This method was deprecated in Gecko 2.0. If you need to monitor low memory conditions, you should watch for the Low memory notifications "memory-pressure" notifications instead.

boolean isLowMemory();
Parameters

None.

Return value

true if we are in a low-memory situation. Otherwise false.

Note:  From Gecko 2.0 always returns false. See bug 592308.

realloc()

Reallocates a block of memory to a new size.

voidPtr realloc(
  in voidPtr ptr,
  in size_t newSize
);
Parameters
ptr
The address of the memory block to reallocate. This may be null, in which case realloc behaves like alloc().
newSize
Specifies the new size in bytes of the block of memory to allocate. This may be 0, in which case realloc behaves like alloc().
Return value

null if the memory allocation fails. Otherwise, it returns a pointer to the newly allocated memory segment. The result must be freed with a call to free() when it is no longer needed.

Low memory notifications

An nsIMemory implementation may be capable of monitoring heap usage. Moreover, a mechanism exists by which a client can receive notifications about low-memory situations.

A client that wishes to be notified of low memory situations (for example, because the client maintains a large memory cache that could be released when memory is tight) should register with the observer service (see nsIObserverService) using the topic "memory-pressure". There are three specific types of notifications that can occur. These types will be passed as the aData parameter of the of the "memory-pressure" notification:

"low-memory"
This will be passed as the value of aData when a low-memory condition occurs (not necessarily an out-of-memory condition).
"heap-minimize"
This will be passed as the value of aData when an explicit call to heapMinimize() was made.
"alloc-failure"
This will be passed as the value of aData when an out-of-memory condition occurs.

When a nsIMemory instance notifies memory pressure observers, it passes itself as the aSubject parameter in the call to nsIObserverService.notifyObservers(). This allows nsIObserver implementations to observe multiple nsIMemory instances and determine the source of memory pressure notifications.

Remarks

This interface was frozen for Gecko 0.9.6. See bug 99151 for details. From Gecko 2.0 interfaces are no longer frozen.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:111 次

字数:8667

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文