dounong5373 2015-09-05 13:32
浏览 51

symfony:表单生成需要大量内存

In my symfony application, I am generating a form in order to parameter some ingredients. I now face the situation where users start managing thousands of ingredients.

With only 200 ingredients, the php memory usage goes above the max 120Mo.

The following shows that the action stats with a usage of 50Mo and that creating the form alone takes 40Mo. It crashes if I try to render the view.

What can I do to avoid forms to take so much memory ?

My controller action :

/**
 * @Route("/set_ingredient_stock", name="set_ingredient_stock")
 * @Method({"PUT","GET"})
 * @Template(":FoodAnalytics/UserIngredient:set_ingredient_stock.html.twig")
 * @param Request $request
 * @Security("is_granted('organizer')")
 * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
 */
public function setIngredientStockAction(Request $request)
{
    r('init : ' . ceil(memory_get_usage() / 1000000));
    $userIngredientManager = $this->get('user_ingredient_manager');
    r('manager : ' . ceil(memory_get_usage() / 1000000));
    /** @var User $user */
    $user = $this->getDoctrine()->getRepository('AppBundle:User\User')->findUserWithUserIngredients($this->getPortfolioUser());
    r('user : ' . ceil(memory_get_usage() / 1000000));
    $setStockIngredientForm = $userIngredientManager->getSetIngredientStockForm($user);
    r('form : ' . ceil(memory_get_usage() / 1000000));

    if ($request->getMethod()=='PUT')
    {
        $formManager = $this->get('form_manager');

        if ($formManager->handleRequestAndFlush($setStockIngredientForm, $user))
        {
            $this->addSuccess("Le stock d'ingrédients à été mis à jour");

            return $this->redirectToRoute('set_ingredient_stock');
        }

        $this->addInvalidFormMessage();
    }

    $nonExecutedTasks = $this->getDoctrine()->getRepository('AppBundle:FoodAnalytics\Task')->getNonExecutedTasksQueryBuilder($this->getPortfolioUser())->select('COUNT(t)')->getQuery()->getSingleScalarResult();
    $nonReceivedOrders = $this->getDoctrine()->getRepository('AppBundle:MarketPlace\Order')->getNonReceivedOrdersQueryBuilder($this->getPortfolioUser())->select('COUNT(o)')->getQuery()->getSingleScalarResult();;
    $horizon = $user->getParameter(User::STOCK_HORIZON) ?: 7;
    r('small_variables : ' . ceil(memory_get_usage() / 1000000));
    $userIngredients = $this->getDoctrine()->getRepository('AppBundle:FoodAnalytics\UserIngredient')->getUserIngredientsWithJoinsIndexedByProductIds($this->getPortfolioUser());
    r('user_ingredients : ' . ceil(memory_get_usage() / 1000000));
    $stocks = $userIngredientManager->getUserIngredientStocks($userIngredients, $horizon);
    ~r('stocks : ' . ceil(memory_get_usage() / 1000000));

    return array(
        'set_stock_ingredient_form' => $setStockIngredientForm->createView(),
        'non_executed_tasks' => $nonExecutedTasks,
        'non_received_orders' => $nonReceivedOrders,
        'stocks' => $stocks,
        'horizon' => $horizon
    );
}

The dumps:

init : 50
manager : 51
user : 61
form : 100
small_variables : 100
user_ingredients : 101
stocks : 105

EDIT : The methods that generates the form just created a regular symfony form attached to the user who owns a collection of userIngredients generated by another type. There are 3 fields per userIngredients.

  • 写回答

1条回答 默认 最新

  • dsfdfd1211 2015-09-06 10:25
    关注

    Not one user is waiting for a list of thousands rules (or items, forms etc). If there are more then about 25 rules then nobody wants to read them anymore.

    You should better provide pagination and Filters

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器