vendredi 2 août 2019

How to induce an out of memory error in java

I'm trying to set up a way to handle out of memory errors in my application. So far, I've added the following options to my Gradle build file:

task appStartScripts(type: CreateStartScripts) {
    def tplName = 'startTemplate.sh'
    assert project.file(tplName).exists()
    defaultJvmOpts = "-XX:+HeapDumpOnOutOfMemoryError",
                      "-XX:HeapDumpPath=\$HOME/log/",
                      "-XX:OnOutOfMemoryError=./\$HOME/application/bin/restart.sh",
                      "-Xms64m", "-Xmx124m"}

Where XX:OnOutOfMemoryError calls the restart script when the an out of memory error occurs. In the restart.sh script itself, I have the following:

while true : do
    java -XX:+ExitOnOutOfMemoryError -jar application.jar
    sleep 5
done

./start.sh

To kill the app, wait 5 seconds, and then call the start script in the same directory to restart the app.

To test this, I added an infinite loop to one of my get requests:

@GET
    @Timed
    @Path("/{ids}")
    public List<Profile> getProfileByID(@PathParam("ids") String ids) {

        boolean forever = true;

        try {
            logger.info("STARTED SERVICE: getProfileID with the following parameters: IDs = {}", ids);
            int[] array = idsStringToArray(ids);

            List<Profile> profileList = profileManager.getProfiles(array);

            List<Integer> myList = new ArrayList<Integer>();

            while (forever) {
                profileManager.getProfiles(array);
                myList.add(1000);
            }

            logger.info("COMPLETED SERVICE: getProfileID with the following parameters: Ids = {}", ids);
            return profileList;
        }

Where myList will continue to have more items added to it, consuming memory.

That said, I haven't been able to cause the error yet to trigger the script. Is there anything else I can do?

Also, here is the result of the top command for the service:

Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.4 us,  1.3 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 32160180 total, 21291040 free,  4559124 used,  6310016 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used. 27127132 avail Mem
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
30504 user1  20   0 3778748 371724  18048 S   4.0  1.2   3:50.33 java

Aucun commentaire:

Enregistrer un commentaire