unsigned int getFreeMemory()
{
	s_mallinfo i = mallinfo();
	unsigned int totalMemory = getTotalMemory();
	unsigned int dynamicTop = EM_ASM_INT_V(return HEAPU32[DYNAMICTOP_PTR>>2]);
	return totalMemory - dynamicTop + i.fordblks;
}
void ofxMemoryUsage::update(){
	
	int secs = (int)(ofGetElapsedTimeMillis()/1000)%60;
	if(updateSecs != secs){
		updateSecs = secs;
		memory_total = getTotalMemory();
		memory_used_total = getUsedMemory();
		memory_used_process = getProcessMemory();		
	}
}
int main()
{
	printf("Before allocation:\n");
	printf("Total memory: %u bytes\n", getTotalMemory());
	printf("Free memory: %u bytes\n", getFreeMemory());
	printf("Used: %u bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
	assert(getTotalMemory() == 16777216);
	assert(getFreeMemory() >= 10000000); // 11529552 in test

	void *ptr = malloc(1024*1024);
	printf("\nAfter 1MB allocation:\n");
	printf("Total memory: %u bytes\n", getTotalMemory());
	printf("Free memory: %u bytes\n", getFreeMemory());
	printf("Used: %u bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
	assert(getTotalMemory() == 16777216);
	assert(getFreeMemory() >= 9000000); // 10480968 in test

	free(ptr);
	printf("\nAfter freeing:\n");
	printf("Total memory: %u bytes\n", getTotalMemory());
	printf("Free memory: %u bytes\n", getFreeMemory());
	printf("Used: %u bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
	assert(getTotalMemory() == 16777216);
	assert(getFreeMemory() >= 10000000); // 11529552 in test

	printf("OK.\n");
}