Created
December 5, 2013 10:57
-
-
Save noppanit/7803500 to your computer and use it in GitHub Desktop.
One quick test to test memory consumption.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.noppanit; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.junit.Test; | |
import java.io.Serializable; | |
import java.util.HashMap; | |
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY; | |
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; | |
public class TestMemoryConsumption { | |
private static final long MEGABYTE = 1024L * 1024L; | |
public static final int LOOP = 1000000; | |
public static long bytesToMegabytes(long bytes) { | |
return bytes / MEGABYTE; | |
} | |
@Test | |
public void testMemoryConsumption() throws Exception { | |
ObjectMapper objectMapper = new ObjectMapper(); | |
HashMap<String, Object> map = new HashMap<String, Object>(); | |
for (int i = 0; i < LOOP; i++) { | |
StoreWithToJson storeWithToJson = new StoreWithToJson("name", "description", objectMapper); | |
map.put(String.valueOf(i), storeWithToJson); | |
} | |
printMemoryConsumption(); | |
} | |
@Test | |
public void testMemoryConsumptionWithoutObjectMapper() throws Exception { | |
HashMap<String, Object> map = new HashMap<String, Object>(); | |
for (int i = 0; i < LOOP; i++) { | |
Store store = new Store("name", "description"); | |
map.put(String.valueOf(i), store); | |
} | |
printMemoryConsumption(); | |
} | |
private void printMemoryConsumption() { | |
Runtime runtime = Runtime.getRuntime(); | |
runtime.gc(); | |
System.runFinalization(); | |
long memory = runtime.totalMemory() - runtime.freeMemory(); | |
System.out.println("Used memory is bytes: " + memory); | |
System.out.println("Used memory is megabytes: " | |
+ bytesToMegabytes(memory)); | |
} | |
} | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = ANY, setterVisibility = NONE) | |
class StoreWithToJson implements Serializable { | |
private String name; | |
private String description; | |
private ObjectMapper mapper; | |
public StoreWithToJson(String name, String description, ObjectMapper mapper) { | |
this.name = name; | |
this.description = description; | |
this.mapper = mapper; | |
} | |
public String toJson() throws JsonProcessingException { | |
return mapper.writeValueAsString(this); | |
} | |
public String getName() { | |
return name; | |
} | |
public String getDescription() { | |
return description; | |
} | |
} | |
class Store implements Serializable { | |
private String name; | |
private String description; | |
public Store(String name, String description) { | |
this.name = name; | |
this.description = description; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getDescription() { | |
return description; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment