I deployed the application using the built-in java version of neo4j under the Jersey tomcat for the REST API. Measuring memory usage with jconsole, I noticed that every REST call adds 200 MB of memory (which, I think, because the entire graph is loaded into memory). Therefore, in just 5 calls, the server allocates 1 GB of memory, which is a lot! To clear the memory, I have to wait for the garbage collector (the threshold is set to 1 GB).
Is this normal behavior because I'm using the built-in java version of neo4j, or am I doing something terribly wrong? What should I do to free up memory when the API call ends?
Here is a sample code:
@GET @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") public Response getApi( @QueryParam("q") String query){ try{ // new neo instance here with EmbeddedGraphDatabase ... some code // stop neo }catch(Exception ex){ // stop neo } return response.ok("json data here").build(); }
Thanks Daniele
-------- FULL CLASS CLASS ----------
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.kernel.EmbeddedGraphDatabase; @Path("/API") public class API { @GET @Produces(MediaType.APPLICATION_JSON) public Response apiCall(@QueryParam("q") String query){ GraphDatabaseService graphDb; try{
And then I call the REST service through the browser as follows http://localhost:8080/API?q=test
UPDATED WITH SINGLET
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.kernel.EmbeddedGraphDatabase; @Path("/API") public class API { @GET @Produces(MediaType.APPLICATION_JSON) public Response apiCall(@QueryParam("q") String query){ GraphDatabaseService graphDb; try{
source share