Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

java callout error

We have one java callout which is generating jwt token in loop
lets say we have 200 objects in a response array, and for each object we are traversing via 3 for loops to add a custom jwt token.

while performing load testing 
one request which is returning more than 2MB response being hit multiple times  getting errored out with below error

Failed to execute JavaCallout. Java heap space

And again its giving response in next hit without any change 

can I know how this java callout works with ApigeeX architecture, where does heap memory getting created for java callouts  and how to increase  @dchiesa1 

0 1 119
1 REPLY 1

May I understand what you're doing?  You wrote your own Java callout, and you're seeing an error?  

And in that single Java callout you are creating a set of 200 JWT ?

That use of the Java callout is outside its intended design point.  The Java callout is not intended to provide a mechanism to host arbitrary application  code.  It is intended as an extensibility mechanism to allow you to do something small and simple associated to one request. Creating a single JWT is a reasonable thing to do in a Java callout (but you have builtin policies for this, so you probably don't need a callout).  Creating 200 , or more, JWT in a loop, with a bunch of other processing, is not a good use of the Java callout. That should be extracted into a microservice. OR an integration platform, like App Integration, which handles longer-running distributed processes. 

Why are you creating 200 JWT anyway?  Maybe you want  to rethink why you are doing this.  

>where does heap memory getting created for java callouts  and how to

Heap memory is just the memory your Java logic is using as it executes.  It gets created by the code you wrote.  If you write sloppy code in your Java callout, it's really easy to exhaust the Java heap.  As an example, you could do something like this: 


    List<Object> largeList = new ArrayList<>();

    // Continuously add objects to the list
    while (true) {
      largeList.add(new byte[1024 * 1024]); // Add 1MB of data
        ...
    }

If you embed logic like that into a Java callout, you will quickly exhaust the heap.  You probably don't have code that is doing something so obvious, but your 4-level nested loop (you said "for each object we are traversing via 3 for loops..." so that would be 4 levels total) could easily accumulate lots of stored data that will sit on the heap. 

So.  Don't do that.  Or, if you must do that, don't try to host that logic in Apigee via a Java callout. Host your Java code in some external system, like Cloud Run, if you want to use the heap so liberally.