Created
July 7, 2022 15:13
-
-
Save Zsailer/74bf74a32b3f9da2a8239e932ef0968f to your computer and use it in GitHub Desktop.
Memory leak in jsonschema
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
import jsonschema | |
import os | |
import psutil | |
import matplotlib.pyplot as plt | |
schema = { | |
"$id": "https://example.com/person.schema.json", | |
"$schema": "https://json-schema.org/draft/2020-12/schema", | |
"title": "Person", | |
"type": "object", | |
"properties": { | |
"firstName": { | |
"type": "string", | |
"description": "The person's first name." | |
}, | |
"lastName": { | |
"type": "string", | |
"description": "The person's last name." | |
}, | |
"age": { | |
"description": "Age in years which must be equal to or greater than zero.", | |
"type": "integer", | |
"minimum": 0 | |
} | |
} | |
} | |
instance = { | |
"firstName": "John", | |
"lastName": "Smith", | |
"age": 40 | |
} | |
memory_usage = [] | |
process = psutil.Process(os.getpid()) | |
iterations = range(0, 1000) | |
for i in iterations: | |
# Call validate from JSON schema. | |
jsonschema.validate(instance, schema) | |
# Track the memory usage in Megabytes. | |
memory = process.memory_info() | |
memory_usage.append(memory.rss / 1e6) | |
fig, ax = plt.subplots() | |
ax.plot(iterations, memory_usage) | |
ax.set_title("Possible memory leak in jsonschema") | |
ax.set_ylabel("Memory Usage (Mb)") | |
ax.set_xlabel("Number of times `validate()` is called.") | |
fig.savefig("test.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment