NS_COM_GLUE nsresult nsMemory::HeapMinimize(bool aImmediate) { nsCOMPtr<nsIMemory> mem; nsresult rv = NS_GetMemoryManager(getter_AddRefs(mem)); NS_ENSURE_SUCCESS(rv, rv); return mem->HeapMinimize(aImmediate); }
NS_COM_GLUE nsIMemory* nsMemory::GetGlobalMemoryService() { nsIMemory* mem; nsresult rv = NS_GetMemoryManager(&mem); if (NS_FAILED(rv)) return nsnull; return mem; }
NS_COM_GLUE nsresult nsMemory::HeapMinimize(bool aImmediate) { nsCOMPtr<nsIMemory> mem; nsresult rv = NS_GetMemoryManager(getter_AddRefs(mem)); if (NS_WARN_IF(NS_FAILED(rv))) return rv; return mem->HeapMinimize(aImmediate); }
nsIMemory* nsMemory::GetGlobalMemoryService() { nsIMemory* mem; nsresult rv = NS_GetMemoryManager(&mem); if (NS_FAILED(rv)) { return nullptr; } return mem; }
nsresult GlueStartupMemory() { NS_ASSERTION(!gMemory, "must be called once"); if (!gMemory) { NS_GetMemoryManager(&gMemory); NS_ASSERTION(gMemory, "can't get memory manager!"); if (!gMemory) return NS_ERROR_FAILURE; } return NS_OK; }
static nsIMemory* SetupGlobalMemory() { NS_ASSERTION(!gMemory, "must be called once"); if (!gMemory) { NS_GetMemoryManager(&gMemory); NS_ASSERTION(gMemory, "can't get memory manager!"); if (gMemory) NS_RegisterXPCOMExitRoutine(FreeGlobalMemory, 0); } return gMemory; }
int main(int argc, char **argv) { nsCOMPtr<nsIMemory> mem; nsresult rv = NS_GetMemoryManager(getter_AddRefs(mem)); if (!mem || NS_FAILED(rv)) { printf("Could not get the memory manager\n"); return -1; } // allocation note. don't use nsIMemory to allocate, // because we want to test the isLowMemory predicate // without regard for the nsIMemory impelmentation (the // implementation might count bytes handed out. however, // the predicate should work with out having to rely on // that. void *big_alloc = malloc(1024 * 1024 * 16); const int highpower = 500000; char* buffers[highpower]; for (int i=0; i<highpower; i++) buffers[i] = nsnull; for (int i=0; i<highpower; i++) { PRBool lowMem = PR_FALSE; size_t s = 4096; //pow(2,i); buffers[i] = (char*) malloc(s); // You have to touch the buffer if (!buffers[i]) printf("Could not allocate a buffer of size %ld\n", s); else { for (int j=0; j<s; j++) buffers[i][j] = 'a'; } PRIntervalTime start = PR_IntervalNow(); mem->IsLowMemory(&lowMem); PRIntervalTime cost = PR_IntervalNow() - start; printf("Total Allocated: %ld. \tLow Memory now? %s\t Took (%d)\n", s*i, lowMem ? "Yes" : "No", PR_IntervalToMilliseconds(cost)); if (lowMem) break; } for(int i=0; i<highpower; i++) { if (buffers[i]) free(buffers[i]); } return 0; }